Skip to content

Bailouts for state updates behavior inconsistently for hooks #17048

@zxh19890103

Description

@zxh19890103

This is a bug I think.

As the title says, React didn't help me to merge the update requests.

const Player = ({ }) => {
  const [name, setName] = useState('John Higgins',)
  const [age, setAge] = useState(47)
  const nextPlayer = () => {
    setName('Mark Selby')
    setAge(34)
  }
  console.log('render.')
  return <p>
    This player's name is {name}, {age} years.
    <button type="button" onClick={nextPlayer}>next</button>
  </p>
}

After the button clicked, it outputs:

>> render.
>> render.

But in fact, I got three.

>> render.
>> render.
>> render.

It doesn't always be.

The actual code

const List = ({}) => {
	const [items, setItems] = useState<ListItem[]>([])
	const [tick, setTick] = useState(0)
	const [page, setPage] = useState(1)
        
        // # effect 001
	useEffect(() => {
           const refresh = tick > 1
           load({ refresh, page })
             .then(d => {
                if (refresh) setItems(d) // replace
                else setItems([...items, ...d]) // append
             })
        }, [tick, page])

        // # effect 002
        useEffect(() => {
            testingScrollToBottom(() => {
               setTick(0) // makes effect 001 applied.
               setPage(page + 1) // makes effect 001 applied again.
            })
            return () => {
                stopTestingScrollToBottom()
            }
        }, [page])
        
        return (
            <div>
                <button
                    onClick={() => {
                        setTick(tick + 1)
                        setPage(1)
                    }}>
                    Refresh
                </button>
                { items.map(item => <Item item={item}/>) }
            </div>
        )
}

Function testingScrollToBottom is where the things happened.

Here is what I did to avoid that:

image

By the way, testingScrollToBottom uses setTimeout.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleAutomatically closed due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions