use hook for rxjs behavior subject
npm install @rcp/use.behaviorsubject
# or use yarn
yarn add @rcp/use.behaviorsubject
import { BehaviorSubject } from 'rxjs'
import { useBehaviorSubject } from '@rcp/use.behaviorsubject'
const subject = new BehaviorSubject({ name: 'imcuttle' })
const App = () => {
const [data, setData] = useBehaviorSubject(subject)
}
import { createReactBehaviorSubject } from '@rcp/use.behaviorsubject'
const { subject, useSubject } = createReactBehaviorSubject({ name: 'imcuttle' })
const App = () => {
const [data, setData] = useSubject()
}
React state pipe to subject, and subject effects to state
import { BehaviorSubject } from 'rxjs'
import { useStateToSubject } from '@rcp/use.behaviorsubject'
const { subject, useSubject } = createReactBehaviorSubject({ name: 'imcuttle' })
const userSubject = new BehaviorSubject({})
const App = () => {
const [user, setUser] = React.useState({ name: 'imcuttle' })
useStateToSubject([user, setUser], userSubject)
}
This library is written and maintained by 余聪, yucong@yuanfudao.com.
MIT