score:2

Accepted answer

The setNumber function accepts a value you want to set your state to. So If you want to set number to the value of newNumber you would do setNumber(newNumber)

score:0

Just use setNumber like

useEffect(() => {
    setNumber(newNumber);
  }, [])

or better just in initial expression

const [number, setNumber] = useState(200)

score:0

Just put your values inside setNumber function.

const [number, setNumber] = useState(0);
  let newNumber = 200;

  useEffect(() => {
    setNumber(newNumber);
  }, [])

score:0

useState describe the state, it can be an object, string, boolean and a number. In case its a primitive data i.e not an object, you just need to pass the value while updating it as follows:

setStateVal(5); // here 5 will become the new state

In case your state is an object, you need to pass the updated state object as follows:

setStateVal({val: 5});  

In no case you use = operator.


Related Query

More Query from same tag