How do I dynamically change styles in react?
-
I need to make it so that, for example, the width property can be passed user input as a value, who can tell me how to do this?JavaScript Jasper Marshall, Feb 8, 2019
-
const [ width, setWidth ] = useState(0);
const onChange = e => setWidth(e.target.value);
const styles = { width: `${width}px` };
<input value={width} onChange={onChange} />
<div style={styles}>Vivian Francis -
const [width, setWidth] = useState (500)
You hang the event on the onChange input and change the state.
On the element you do this:
<div style={{width: `${width}px`}}> ширина блока <\div>
Should workAnonymous
2 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!