score:0
So a solution here could be something like this:
In your stylesheet file:
const bgColors = {
primary: primaryColor[0],
warning: warningColor[0],
danger: dangerColor[0],
success: successColor[0],
info: infoColor[0],
rose: roseColor[0],
gray: grayColor[0]
}
export type bgColorKey = keyof typeof bgColors;
interface Props {
color: bgColorKey;
}
export const badgeStyles = createStyles({
badge: ({ color }: Props) => ({
borderRadius: "12px",
padding: "5px 12px",
textTransform: "uppercase",
fontSize: "10px",
fontWeight: 700,
lineHeight: 1,
color: whiteColor,
textAlign: "center",
verticalAlign: "baseline",
display: "inline-block",
backgroundColor: bgColors[color]
})
});
What I did here is to list all the colors mapping in a separate object bgColors
(which really could take place in an assets
folder in root folder), that you can access in your stylesheet.
Then you have to declare a type bgColorKey
which will be used to type the color
variable in order to index bgColors
. It's simply a keyof typeof bgColors
which basically tells typescript "take the type of my object bgColors
and make a union of all its keys as strings".
The thing to know here is that you can pass props to useStyles
, in order to make some dynamic styling. So in you badge
section of your stylesheet, we pass a callback which takes props as parameters, inside those props we have the color
property from your component (which is of type bgColorKey
which is the union of "primary"
, "warning"
, "rose"
, and so on).
At that point we simply add backgroundColor: bgColors[color]
in the badge
section.
So in your component you may have something like this:
export default function MyComponent() {
const color: bgColorKey = 'primary';
const useStyles = makeStyles(badgeStyles);
const classes = useStyles({ color });
return (
<span className={classes.badge}>{children}</span>
)
}
We pass color
to useStyles
, and your stylesheet will know which color actually correspond to your color
variable.
Note that you could get rid of createStyles
and export useStyles
directly from your stylesheet file like so :
export const useStyles = makeStyles(theme => ({
badge ({ color }: Props) => ({
// ...
})
})
and import it from your component like so:
import useStyles from './styles.tsx';
// ...
const classes = useStyles({ color });
Source: stackoverflow.com
Related Query
- react typescript error - element implicitly has an any type because expression of type string cant be used to index type {}
- TS | Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<SecurityMode, boolean>'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Palette'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type React Typescript
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{...}'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Breakpoints'
- how fix Element implicitly has an 'any' type because expression of type 'string' can't be used to index type?
- Global window object: Element implicitly has an 'any' type because index expression is not of type 'number'.ts
- TypeScript - Element implicitly has an 'any' type because expression of type '"storyFile"' can't be used to index type '{}'
- Typescript with React > Element implicitly has an 'any' type because expression of type 'string' can't be used to index
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Item'
- Nested Object -> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{}' - React Anagram
- Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type
- error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index 'ModuleType'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Number'
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '{}'
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ 1: Element; 2: Element; 3: Element; }'. TS7053
- Element implicitly has an 'any' type because expression of type 'any' can't be used to index type with createStyles
- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BGColors'
- Typescript error in map(): Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
- error TS2602: JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist
- TypeScript Err: "Element implicitly has an 'any' type because expression of type 'any' can't be used to index type
- Typescript-React State: Element implicitly has an 'any' type because type 'State' has no index signature
- How do I fix the error "Element implicitly has an 'any' type because the expression of type
- React Typescript: Element implicitly has an 'any' type because type has no index signature
- element implicitly has an 'any' type because type 'yyy' has no index signature.ts(7017)
More Query from same tag
- How to send data from Modal, Form using react and bootstrap?
- React form validation for multiple field
- Showing dynamic URL on previous and next page using React
- How to create selects and states dynamically on react using Material UI?
- how to not add a user if it exists in a form react.js
- Modal using Bootstrap doesn't update content
- React-Router: Nested route not rendering component
- How to display different tooltip text while mapping over a list?
- Async Await is not working as expected: ReactJS
- What's wrong with my get route? I'm receiving a 401 status code when I try to run my get route
- Is there a way to set the initialValues using state with formik?
- Use react component as a child or from within the render function of a React component
- I can't define type to set an array of objects in a variable React.useState
- React.js. How to change keyword 'this'
- Some of the tailwind classes doesn't apply
- Access constructor variables inside the render method - Reactjs
- React react-router-dom question how to have "/" treated as a string in the url route
- create-react-app Error: Cannot find module 'react-scripts/scripts/init.js'
- Megre results of one API call with another React.js Javascript
- How to display data dynamically with ant design tables
- How to truncate a string in Reactjs and Tailwind CSS
- Calculating price returning NaN in React
- Mapbox-gl in React.js app, TypeError: Cannot read property 'setFeatureState' of undefined
- TouchableOpacity and onPress for Icons
- tiptap font size react
- react material ui Textfield (type=number) validate 10 characters are existing
- setState in async function
- Trouble rendering html in react
- React component default props with graphql
- In Mui, how do I use theme values in my css