-
|
Hello! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
|
I would suggest using CSS variables for your colors. You can then at runtime of your project, dynamically generate the colors as CSS variable values from the backend. |
Beta Was this translation helpful? Give feedback.
-
|
If you're already using ReactJs, The reason is that ReactJs would automatically rerender the elements, Here's an example of using ReactJs Native State Hook. class Example extends React.Component {
constructor(props) {
super(props);
// initialize state hook
this.state = {
element_color: "#FFFFFF"
};
}
async componentDidMount() {
// request api
const api_return = await fetch("https://example.com").then(response => response.json());
// update state hook value
this.setState({element_color: api_return.color});
}
render() {
// this method is called whenever reactjs rerenders this element
// the style={} applies style directly onto the element, without the use of CSS variables
return <div style={{backgroundColor: this.state.element_color}}>
{`This element has the color ${this.state.element_color}`}
</div>;
}
} |
Beta Was this translation helpful? Give feedback.
I would suggest using CSS variables for your colors. You can then at runtime of your project, dynamically generate the colors as CSS variable values from the backend.