How do I dynamically change styles in react?

  • 0
    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

  • 2 Answers
  • 0
    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

  • 0
    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 work
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!