30

I would like to know if I can use setState hook multiple times in same function. For example, like this

import React, { useEffect, useState } from 'react';

function(props) {
const [color, setColor] = useState(0)
const [size, setSize]= useState(0)
const [weight, setWeight] = useState(0)

const onClickRandomButton = () => {
    setColor(Math.random() * 10)
    setSize(Math.random() * 10)
    setWeight(Math.random() * 10)
}

return <div>
  <button onClick = {onClickRandomButton}>random</button>
</div>

}

I have tested, but it doesn't work as expected. To set multiple values at once using hook, how should I do? Thanks

11
  • 1
    Where is the useState part? Commented Feb 28, 2020 at 1:35
  • sorry, it was typo Commented Feb 28, 2020 at 1:36
  • 1
    is conClickRandomButton name also a typo? Commented Feb 28, 2020 at 1:36
  • 2
    Code works as expected: codesandbox.io/s/dazzling-hill-1ypn7 ? Commented Feb 28, 2020 at 1:40
  • 2
    Remember that setting state in React is asynchronous. If you try to operate on the new value in that same event handling function there is no guarantee that the state will have finished updating. Commented Feb 28, 2020 at 1:52

2 Answers 2

29

You can use one useState with object value for updating the styles at once:

import React, { useEffect, useState } from 'react';

export default function (props) {
  const [style, setStyle] = useState({ color: 0, size: 0, weight: 0 });

  const onClickRandomButton = () => {
    setStyle({
      color: Math.random() * 10,
      size: Math.random() * 10,
      weight: Math.random() * 10,
    });
  };
  
  return (
    <div>
      <button onClick={onClickRandomButton}>random</button>
    </div>
  );
}

And if in any method you want to update just one property, for example: color, you could do something like this:

...
  const handleEditColor = color => {
    setStyle({
      ...style,
      color
    });
  };

  // or

  const handleEditColor = color => {
    setStyle(prevState => ({
      ...prevState,
      color,
    }));
  };
...
Sign up to request clarification or add additional context in comments.

1 Comment

You should be careful of using this approach as it is always getting new value for set state function. In some rare cases, like inside useContext, it could fall into infinite loop.
6

I believe unstable_batchUpdates will works for hooks as well as it works for class-based components. Besides prefix unstable_ it's mentioned by Dan Abramov and in React-Redux docs so I consider it's safe to use it:

import { unstable_batchUpdates } from 'react-dom';
...

const onClickRandomButton = () => unstable_batchUpdates(() => {
    setColor(Math.random() * 10)
    setSize(Math.random() * 10)
    setWeight(Math.random() * 10)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.