0.11.0 • Published 2 months ago

@czaplej/use-behavior-subject v0.11.0

Weekly downloads
20
License
MIT
Repository
github
Last release
2 months ago

@czaplej/use-behavior-subject

license npm latest package

It's alternative react state management uses [rxjs]

Explanation

Coming soon...

Demo

https://codesandbox.io/s/distracted-water-e9b1p

Usage

App.tsx

import {
  useBehaviorSubjectDispatch,
  useBehaviorSubjectSelector,
} from '@czaplej/use-behavior-subject';
import * as React from 'react';
import './styles.css';
import { subject$ } from './global-store';
import { Child } from './child';

export default function App() {
  const store = useBehaviorSubjectSelector((state) => state, subject$);
  const dispatch = useBehaviorSubjectDispatch(subject$);
  return (
    <div className="App">
      <h1>PARENT</h1>
      <h2>{JSON.stringify(store)}</h2>
      <button
        onClick={(e) => {
          dispatch({ showChild: !store.showChild });
        }}
      >
        show Child
      </button>
      {store.showChild && <Child />}
    </div>
  );
}

Child.tsx

import React from 'react';
import {
  useBehaviorSubjectDispatch,
  useBehaviorSubjectSelector,
} from '@czaplej/use-behavior-subject';
import { BehaviorSubject } from 'rxjs';
import { subject$ } from './global-store';
type ChildStore = {
  firstName: string;
  lastName: string;
  phone?: number;
  showChild?: boolean;
};

const childSubject$ = new BehaviorSubject<ChildStore>({
  firstName: 'Child',
  lastName: 'child Data',
});
export function Child() {
  const store = useBehaviorSubjectSelector((state) => state, subject$);
  const dispatch = useBehaviorSubjectDispatch(subject$);
  const childStore = useBehaviorSubjectSelector(
    (state) => state,
    childSubject$
  );
  const dispatchChild = useBehaviorSubjectDispatch(childSubject$);
  return (
    <div className="App">
      <h1>Child</h1>
      <h2>{JSON.stringify(store)}</h2>
      <label htmlFor="firstName">
        firstName
        <input
          type="text"
          value={store.firstName}
          onChange={(e) => dispatch({ firstName: e.currentTarget.value })}
        />
      </label>
      <h3>CHILD STORE {JSON.stringify(childStore)}</h3>
      <label htmlFor="firstName">
        firstName
        <input
          type="text"
          value={childStore.firstName}
          onChange={(e) => dispatchChild({ firstName: e.currentTarget.value })}
        />
      </label>
    </div>
  );
}

global-store.ts

import { BehaviorSubject } from 'rxjs';

export type Store = {
  firstName: string;
  lastName: string;
  phone?: number;
  showChild?: boolean;
};
export const subject$ = new BehaviorSubject<Store>({
  firstName: 'newbie',
  lastName: 'lastName newbie',
});

Rest coming soon... Please check the demo at this moment

This library was generated with Nx.

0.11.0

2 months ago

0.10.1

4 months ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.0

3 years ago

0.5.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.3.0

3 years ago

0.4.0

3 years ago

0.2.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago