最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do we still need functional setState way in react hooks? - Stack Overflow

programmeradmin1浏览0评论
  const [count, setCount] = useState(0);

  const handleClick = () =>
    setCount(prevCount => {
      return prevCount + 1;
    });
  const [count, setCount] = useState(0);

  const handleClick = () => setCount(count + 1);

Coming from class-based component background, it becoming a habit where we use functional setState. I'm wondering if do we still need to rely on prevState in functional hooks? Or the current state is always "trustable" and most "updated"?

  const [count, setCount] = useState(0);

  const handleClick = () =>
    setCount(prevCount => {
      return prevCount + 1;
    });
  const [count, setCount] = useState(0);

  const handleClick = () => setCount(count + 1);

Coming from class-based component background, it becoming a habit where we use functional setState. I'm wondering if do we still need to rely on prevState in functional hooks? Or the current state is always "trustable" and most "updated"?

Share Improve this question edited Dec 6, 2019 at 21:08 Emile Bergeron 17.4k5 gold badges84 silver badges131 bronze badges asked Apr 4, 2019 at 7:49 IsaacIsaac 12.9k17 gold badges63 silver badges132 bronze badges 1
  • 1 Yes, we do. No, current state isn't trustable, less than ever before. See for example, stackoverflow.com/questions/53845595/… – Estus Flask Commented Apr 4, 2019 at 7:54
Add a comment  | 

2 Answers 2

Reset to default 24

Yes, the behavior is similar.

React is batching the updates calls. When Writing:

const handleClick = () => setCount(count + 1)
handleClick()
handleClick()
handleClick()

the count in state will be 1

When Writing:

const handleClick = () =>
  setCount(prevCount => {
    return prevCount + 1;
});
handleClick()
handleClick()
handleClick()

the count in state will be 3

State updater function is necessary in both class and functional components. this.setState shouldn't be used together with this.state, the same applies to useState state and state setter. There are more cases for useState when not using state updater will result in wrong behaviour.

In class components, the only problem with the use of this.state is race condition due to asynchronous state updates:

componentDidMount() {
  this.setState({ count: this.state.count + 1 });
  this.setState({ count: this.state.count + 1 }); // overwrites with stale count
  console.log(this.state.count); // not updated
}

When there are no race conditions, this.state can be accessed anywhere inside a component because this reference stays the same:

componentDidMount() {
  this.setState({ count: this.state.count + 1 });

  setTimeout(() => {
    this.setState({ count: this.state.count + 1 });
  }, 100)

  setTimeout(() => {
    console.log(this.state.count);
  }, 200)
}

In functional components, the problem with the use of useState state is function scope. There's no object like this that could be accessed by reference, a state is accessed by value which won't be updated until a component is re-rendered:

const [count, setCount] = useState(0);

useEffect(() => {
  // runs once on mount
  // count is always 0 in this function scope

  setCount({ count: count + 1 });

  setTimeout(() => {
    setCount({ count: count + 1 }); // overwrites with stale count
  }, 100)

  setTimeout(() => {
    console.log(count); // not updated
  }, 200)
}, []);
发布评论

评论列表(0)

  1. 暂无评论