Skip to content

Instantly share code, notes, and snippets.

@FaiChou
Created February 25, 2022 05:45
Show Gist options
  • Save FaiChou/4cbdf57e55b34d3234be9a10daa78816 to your computer and use it in GitHub Desktop.
Save FaiChou/4cbdf57e55b34d3234be9a10daa78816 to your computer and use it in GitHub Desktop.
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