I am from the javascript
world and am new to typescript
. I have a react project written in typescript
. I declare an inline style but got below warning when I use it in a react component:
Type '{ color: string; fontSize: string; fontWeight: number; }' is not assignable to type 'CSSProperties'.
Below is the code for the style declaration.
const styles = {
promAlert: {
display: 'flex',
flexDirection: 'column'
}
};
below is the code who uses it. And the warning comes out with this line <div style={styles.promAlert}>
.
<div style={styles.promAlert}>
{alert.substring('promotions.'.length)}
</div>
I have searched that the this may be caused by CSSProperties
defined in react. I may need to extend this class and add more attributes on it. I wonder how I can do that in my project.
Another question is why CSSProperties
doesn't include all supported css keys.
Use the React. CSSProperties type to pass CSS styles as props in React TypeScript, e.g. style: React. CSSProperties; . The CSSProperties type is used to type the style object that consists of CSS property names and values.
Set the onMouseEnter and onMouseLeave props on the element. When the user hovers over or out of the element, update a state variable. Conditionally set inline styles on the element.
Conditional Changing the Background Color in Reactimport React from 'react'; import './App. css'; function App() { const isBackgroundRed = true; return ( <div className={isBackgroundRed ? 'background-red' : 'background-blue'} /> ); } export default App; JSX allows us to write JavaScript inside of HTML.
To change background color on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the background color based on the state variable.
TypeScript expects specific types "flex"
and "column"
, but without any additional work, it receives string
for both, which is too wide and non-descript. You can see the same error happen here:
declare const value: string
const a: "a" = value // errors, `string` can't be assigned to the more specific `"a"`
There are a few solutions here. I think the most elegant one is as const
, so it'll treat your object values as their specific literal types. Also make sure your TS version is up to date to use this.
const styles = {
promAlert: {
display: 'flex',
flexDirection: 'column',
},
} as const
Alternatively, you can declare your style objects separately and have them typechecked by the CSSProperties
interface. This gives autocomplete on each property, but this can't (easily) be done as a single full object.
const promptAlert: React.CSSProperties = {
display: 'flex',
flexDirection: 'column'
}
// you can re-assign all of your styles into a single object if you want,
// but I think it would be easier to access `promptAlert` directly at this point
const styles = {
promptAlert,
}
Other less ideal solutions:
{ [key: string]: React.CSSProperties }
: this doesn't perform type checking on the name of each style, so you can do styles.abcdef
without any error
Casting each style as React.CSSProperties
: this doesn't catch errors in the styles themselves, e.g. you could typo display: "fleex"
Defining styles
type as shown below will preserve type check on your all CSS classes and their properties:
const styles: { [key: string]: React.CSSProperties } = {
promAlert: {
display: 'flex',
flexDirection: 'column'
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With