Created
April 12, 2020 08:02
-
-
Save seivan/e86c3a67bc85cbb1ce09b0ce15ddf048 to your computer and use it in GitHub Desktop.
mobx-react-hook
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
const WithJustAutoRun = React.memo((props: { | |
first: Todo; | |
second: Todo; | |
counter: number; | |
}) => { | |
const rendered = useObservation(() => { | |
return ( | |
<div> | |
<h1> | |
Obs: {props.counter.toString() + props.first.completed.toString()} | |
</h1> | |
</div> | |
); | |
}, [props]); | |
return <div>{rendered}</div>; | |
}) |
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
const useObservation = ( | |
render: () => JSX.Element, | |
dependencies: React.DependencyList = [] | |
) => { | |
const ref = useRef<JSX.Element>(); | |
const [tick, setTick] = useState(0); | |
useEffect(() => { | |
return autorun(() => { | |
ref.current = render(); | |
setTick(tick + 1); | |
}); | |
}, dependencies); | |
return ref.current; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment