Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

执行 setData ,整个 hooks组件 会重新渲染多次 #31

Open
CHHHAN opened this issue Jul 26, 2022 · 3 comments
Open

执行 setData ,整个 hooks组件 会重新渲染多次 #31

CHHHAN opened this issue Jul 26, 2022 · 3 comments

Comments

@CHHHAN
Copy link

CHHHAN commented Jul 26, 2022

import AppStorages from '../../Storages/AppStorage'

const Amap = (props) => {

  const UI = useStore(UIStorages)
  const App = useStore(AppStorages)
  let _AMap, _map = null
  let i = 0;

// ...somecode
  return <>
  {console.log(`刷新:${ i+1 }`)}
    <APILoader akay="">
      <div style={{ width: '100%', height: document.body.clientHeight - 154 }}>
        <Map zoom={8} zooms={[2, 20]} center={[]}
          onComplete={() => {
            _map.panTo([], 800)
            _map.setZoom(13, false, 1500)
          }}
          onZoomEnd={() => {
            if (_map.getZoom() > 14) {
              if(App.Amap_type === 0){
                console.log("0")
                App.setAmap_type(1)
              }
            } else {
              if(App.Amap_type === 1){
                console.log("1")
                App.setAmap_type(0)
              }
            }
          }}
        >
// ...somecode

订阅已开启memo,但是执行到 App.setAmap_type(1) 会 console.log() 输出 "刷新:1",i 都是 1.

@awmleer
Copy link
Owner

awmleer commented Jul 26, 2022

在这里写个更具体的复现 demo 吧:

https://codesandbox.io/s/github/awmleer/reto-demo/tree/master/?fontsize=14

@CHHHAN
Copy link
Author

CHHHAN commented Aug 25, 2022

@hchlq
Copy link

hchlq commented Oct 10, 2022

好像确实存在这个问题,这个例子会更好理解一些 https://codesandbox.io/s/react-forked-n012dl?file=/App.js:123-381

如果使用 AppContext.Provider,不会渲染两次 https://codesandbox.io/s/react-forked-vq092y?file=/App.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants