RubyLouvre / anu

the React16-compat library with hooks
https://rubylouvre.github.io/anu/
Apache License 2.0
3.19k stars 320 forks source link

[bugfix] useCallback should always return create function #1106

Closed lizheming closed 5 years ago

lizheming commented 5 years ago

As documentation said, useCallback should always return a memoized callback. Here is an example that different between React and Anujs:

<script src="https://unpkg.com/anujs@1.5.6/dist/React.js"></script>
<div id="app"></div>
<script>
const App = () => {
  const [count, up] = React.useState(1);
  const fn = React.useCallback(() => count + 10, []);
  const onClick = () => up(count + 1);

  return (
    <React.Fragment>
      <button onClick={onClick}>click me</button>
      <div>{`count value:${count},count value after plus:${fn()}`}</div>
    </React.Fragment>
  );
}

ReactDOM.render(<App />, document.getElementById('app'));
</script>

The reason why it returns error is that #1083 pull request fix to return undefined when deps equals, that's all my fault. So I revert #1083 pull request and change useEffectImpl method to avoid #1067 problem.