Sanchez86 / mc2

0 stars 0 forks source link

can't expect that setState will work synchronously #1

Open indeyets opened 2 months ago

indeyets commented 2 months ago

https://github.com/Sanchez86/mc2/blob/49a71ad05e513de395c0e8ea047d5e4e2e604a45/src/components/TodoAdd/TodoAdd.jsx#L23-L29

setState is an asynchronous operation. there is a chance, that you will read old value of title here.

either read from the source value or use a callback

Sanchez86 commented 2 months ago

ok. i will rewrite with help callback fn

handleTitle = (e) => {
    this.setState({ title: e.target.value }, () => {
      if (this.state.title.length > 1) {
        this.setState({ isActive: false });
      } else {
        this.setState({ isActive: true });
      }
    });
  };