N
N
Nikita Stechkin2020-10-31 17:39:30
React
Nikita Stechkin, 2020-10-31 17:39:30

How to make style changes dynamic 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?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-10-31
@VAMPIRE37

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}>

G
Gary_Ihar, 2020-10-31
@Gary_Ihar

const [width, setWidth] = useState(500)
You hang an event on the onChange input and change the state.
On an element, do this:

<div style={{width: `${width}px`}}> ширина блока <\div>

Should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question