You are not using the props as far as I can see and besides that props are immutable. You might want to look into state and use that as the backGroundColor but for inline styling it could look like this
<div style={{color: condition ? "red": "green"}}> </div>
EDIT: Seems like there is no styling intended for the NavItem. See here. You have to use css for that e.g. Change the <a> element class in react-bootstrap NavItem
Support using `.css?inline` imports in TypeScript
React Tsx with React Bootstrap - Inline CSS for Active NavItem - Stack Overflow
reactjs - Typescript error during inline fontWeight style in React - Stack Overflow
reactjs - How to define css variables in style attribute in React and TypeScript - Stack Overflow
Videos
» npm install @types/inline-css
You are not using the props as far as I can see and besides that props are immutable. You might want to look into state and use that as the backGroundColor but for inline styling it could look like this
<div style={{color: condition ? "red": "green"}}> </div>
EDIT: Seems like there is no styling intended for the NavItem. See here. You have to use css for that e.g. Change the <a> element class in react-bootstrap NavItem
Solved it like this, not the prettiest solution but works fine.
const css = `
.route-list .nav-tabs>li.active>a {
background-color: green;
color: white;
}
`
<main>
<div className="route-list">
<style>{css}</style>
<Nav bsStyle="tabs" activeKey="1">
<LinkContainer to="/about">
<NavItem eventKey="1">Om</NavItem>
</LinkContainer>
<LinkContainer to="/facts">
<NavItem eventKey="2">Fakta</NavItem>
</LinkContainer>
</Nav>
</div>
</main>
Typescript can be awfuly silly sometimes. It's inferring the type of fontWeight as a string, even though it could narrow it to its exact literal. You can just cast it as itself to fix this:
const boldText = {
fontWeight: 'bold' as 'bold'
}
<td style={boldText}>Content</td>
These days you can also use the new as const declaration at the end of your literals for the same effect:
const boldText = {
fontWeight: 'bold'
} as const;
<td style={boldText}>Content</td>
And finally, you can always provide an explicit type to the object when you declare it:
const boldText: React.CSSProperties = {
fontWeight: "bold"
};
<td style={boldText}>Content</td>
Typescript sees boldText as a "normal" object, and so infers boldText.fontWeight as string:
const boldText = {
fontWeight: 'bold' // <-- TS doesn't know more than this is a string
}
The code <td style={boldText}>Content</td> expects CSSProperties, so a string can't be assigned.
To solve the mismatch, you can tell Typescript the type of the object:
const boldText: CSSProperties = {
fontWeight: 'bold'
}
To be more precise:
- e.g.
color: '#ff0000'would work, becausecoloraccepts values of type string. fontWeight: 'bold'doesn't work, althoughfontWeightaccepts"bold", because TS decides to use the type string before it knows about the context of CSS, and the type string is not the same as the type "bold" | "bolder" | 100 | 200 | ...:
const boldText = {
fontWeight: 'bold' // <-- TS makes it's decision for type 'string'
color: '#ff0000' // <-- TS makes it's decision for type 'string'
}
// TS doesn't complain about color, as color accepts NamedColor | string | ...
// TS only complains about fontWeight, as it accepts only "bold" | "bolder" | 100 | 200 | ...
const TD = <td style={boldText}>Content</td>
Like this:
function Component() {
const style = { "--my-css-var": 10 } as React.CSSProperties;
return <div style={style}>...</div>
}
Or without the extra style variable:
function Component() {
return <div style={{ "--my-css-var": 10 } as React.CSSProperties} />
}
This works in both .tsx files and .d.ts files.
The key is to include import 'react' at the top. This turns the file into a module (even if it is a .d.ts file), allowing you to use Module Augmentation to extend the existing React types rather than overwriting them.
The Solution
Place this snippet at the top of your component file (.tsx) or in any definition file (e.g., types.d.ts).
import 'react';
declare module 'react' {
interface CSSProperties {
// Allow any CSS variable starting with '--'
[key: `--${string}`]: string | number
}
}
Usage
Now you can pass CSS variables to the style prop without TypeScript errors:
<div style={{ "--value": percentage, color: "var(--value)" }} />
» npm install inline-css
What about using ngStyle
customeStyle =
{
'color': 'red'
}
;
<p [ngStyle]="customeStyle">asdasd</p>
Instead of querying the DOM, you can use Angular Style binding
Template
<h1 [style.color]="isHoussein() ? 'red': 'black'">{{firstname}}</h1>
Name: <input type="text" [(ngModel)]="firstname">
<button (click)="changeName()">Change Name</button>
Component.ts
export class MyApp {
firstname: string = 'Jimmy';
changeName() {
this.firstname = 'Houssein';
}
isHoussein() {
return this.firstname === 'Houssein';
}
}
I am trying to create a component that sets CSS Variables using the inline style attribute. Example:
<div style={{'--item-color': props.color}}>...</div>
Is this possible with react? I can't find any documentation or previous conversation on the topic. All I can find is using DOM CSS properties like backgroundColor: ....
The above example would give the following error:
Type '{ '--item-color': string; }' is not assignable to type 'Properties<string | number, string & {}>'.Object literal may only specify known properties, and ''--item-color'' does not exist in type 'Properties<string | number, string & {}>'.ts(2322)