Last active
January 22, 2023 13:10
-
-
Save JonathanTurnock/e63aeac80a04993fc588aa6961f84b52 to your computer and use it in GitHub Desktop.
React RXJS Subject State https://playcode.io/1088821
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'; | |
import { BehaviorSubject } from 'rxjs'; | |
import { useObservable } from 'react-use'; | |
const subject = new BehaviorSubject(1); | |
const useSubjectState = (subject) => [useObservable(subject), (value) => subject.next(value)] | |
const Increment = () => { | |
const [state, setState] = useSubjectState(subject); | |
return <div><button onClick={() => setState(state + 1)}>increment</button><p>{state}</p></div> | |
} | |
export const App = (props) => { | |
return ( | |
<div className='App'> | |
<Increment /> | |
<Increment /> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment