reto
reto copied to clipboard
执行 setData ,整个 hooks组件 会重新渲染多次
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.
在这里写个更具体的复现 demo 吧:
https://codesandbox.io/s/github/awmleer/reto-demo/tree/master/?fontsize=14
在这里写个更具体的复现 demo 吧:
https://codesandbox.io/s/github/awmleer/reto-demo/tree/master/?fontsize=14
久等了! 这是 demo: https://codesandbox.io/s/react-forked-dgqlhf?file=/Templates/Home/index.js
https://dgqlhf.csb.app
好像确实存在这个问题,这个例子会更好理解一些 https://codesandbox.io/s/react-forked-n012dl?file=/App.js:123-381
如果使用 AppContext.Provider,不会渲染两次 https://codesandbox.io/s/react-forked-vq092y?file=/App.js