Skip to content

Instantly share code, notes, and snippets.

@JonathanTurnock
Last active January 22, 2023 13:10
Show Gist options
  • Save JonathanTurnock/e63aeac80a04993fc588aa6961f84b52 to your computer and use it in GitHub Desktop.
Save JonathanTurnock/e63aeac80a04993fc588aa6961f84b52 to your computer and use it in GitHub Desktop.
React RXJS Subject State https://playcode.io/1088821
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