There are several strategies to change colors of some element with React but, yours was not the best choice, because, is mixing commands, components in React need to have global or local states for change, a basic example for changes in a div
making toggle
(true or false) and changing the color:
1 ) Naturally shaped React state
function App() {
const [toogle, setToogle] = React.useState(true);
const [cor, setCor] = React.useState('#c3c3c3');
React.useEffect(() => {
setCor((state) => toogle ? '#c3c3c3': '#446677');
}, [toogle]);
return (
<div>
<button
onClick={e =>
setToogle(state => !state)}
>Alterar</button>
<br /><br />
<div style={{
height: '200px',
width: '200px',
backgroundColor:cor,
}}>
</div>
</div>
)
}
ReactDOM.render( <App/> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>
<div id="root">Carregando ...</div>
where the state will control the two colors that were stipulated in that code.
2 ) Reference of the element:
By reference of div we can also change colors of any screen element, example:
function App() {
const refDiv = React.useRef()
const [toogle, setToogle] = React.useState(true);
React.useEffect(() => {
if (refDiv.current) {
refDiv.current.style.backgroundColor = '#c3c3c3';
refDiv.current.style.height = '200px';
refDiv.current.style.width = '200px'
}
}, []);
React.useEffect(() => {
if (refDiv.current) {
if (toogle) {
refDiv.current.style.backgroundColor = '#c3c3c3';
} else {
refDiv.current.style.backgroundColor = '#446677';
}
}
}, [toogle]);
return (
<div>
<button
onClick={e =>
setToogle(state => !state)}
>Alterar</button>
<br /><br />
<div ref={refDiv}>
</div>
</div>
)
}
ReactDOM.render( <App/> , document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>
<div id="root">Carregando ...</div>
is an unusual way, particularly not user, but, this way found to access the <div>
by reference, some events are even cool to use for example put focus()
in elements <input />
, then the first option is excellent and does not cause you future problems.
3 ) With Styled Components: website
This form is widely used to stylize components of React dynamically by properties the element is changed:
const DivColor = window.styled.div`
background: ${({toggle}) => toggle ? '#c3c3c3': '#446677'};
height: 200px;
width: 200px;
`;
function App() {
const [toggle, setToggle] = React.useState(true);
return (
<div>
<button
onClick={e =>
setToggle(state => !state)}
>Alterar</button>
<br /><br />
<DivColor toggle={toggle}></DivColor>
</div>
)
}
ReactDOM.render( <App/> , document.getElementById('root'));
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script><script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script><script src="https://unpkg.com/react-is/umd/react-is.production.min.js"></script><script src="https://unpkg.com/styled-components/dist/styled-components.min.js"></script>
<div id="root">Carregando ...</div>
The 3 forms work, the 1 and the 3 are the most recommended for working with state and commands of the React
to 2 are more for events or something specific, but as said not much used for stylization.
Entendiii! What if I had something like this: const [active, setActive] = useState({ card1: false, card2: false, card3: false, card4: false, card5: false, card6: false, card7: false, }); and I had <div classname={
${active.id ? 'active' : ''}
} id={id} > {title} </div> in which this "id" was card1 or card2 etc... it did not recognize id as card1 card2, as I could solve Iss?– Rindi
Any doubt beyond your original question automatically generates a new question for you @Rindi! does this generate a new question with that comment and clear clarifies as much as possible
– novic
But I tried to do it and they said it was a duplicate question of this now, I didn’t understand
– Rindi