Created
February 25, 2022 05:45
-
-
Save FaiChou/4cbdf57e55b34d3234be9a10daa78816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
var obj = null; | |
var listener = null; | |
function getObj() { | |
return obj; | |
} | |
function setListener(l) { | |
listener = l; | |
} | |
function setObj(o) { | |
obj = o; | |
if (listener) { listener(o); } | |
} | |
function Child1() { | |
return ( | |
<div>{JSON.stringify(getObj())}</div> | |
) | |
} | |
function Chld2() { | |
const setRandom = () => { | |
setObj({ val: Math.random().toString() }); | |
} | |
return ( | |
<button onClick={setRandom}>click</button> | |
) | |
} | |
export default function App() { | |
const [, forceUpdate] = React.useReducer(x=>x+1,0); | |
React.useEffect(() => { | |
setListener((newObj) => { | |
forceUpdate(); | |
console.log(newObj); | |
}) | |
}, []) | |
return ( | |
<div> | |
<Child1 /> | |
<Chld2 /> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment