reach / reach-ui

The Accessible Foundation for React Apps and Design Systems
https://reacttraining.com/reach-ui/
MIT License
5.97k stars 559 forks source link

menu-button: onSelect fires twice when code is wrapped in StrictMode #440

Closed coreylight closed 4 years ago

coreylight commented 4 years ago

🐛 Bug report

Current Behavior

Expected behavior

Reproducible example

https://codesandbox.io/s/pedantic-varahamihira-i5e0f

Suggested solution(s)

??

Your environment

Software Name(s) Version
Reach Package menu-button 0.7.3
React 16.12.0
Browser https://whatsmybrowser.org/b/GHMENEL
chaance commented 4 years ago

At first glance I ... have no idea what is going on here!

elrypto commented 4 years ago

React strict mode will double invoke certain lifecycle methods, it is a feature to help figure out issues in an application. Those methods are constructor, render, setState, getDerivedStateFromProps. https://reactjs.org/docs/strict-mode.html.

Cryrivers commented 4 years ago

I think the root cause is the side effect in menu reducer. The reducer calls callbacks (i.e. onSelect). However in Concurrent React, we cannot guarantee internal React hooks (esp. useState, useReducer) to run only once.