New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bloc-react

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bloc-react

[![codecov](https://codecov.io/gh/jsnanigans/bloc-react/branch/main/graph/badge.svg?token=0FPH6ZMZD3)](https://codecov.io/gh/jsnanigans/bloc-react) [![liscence](https://img.shields.io/badge/license-MIT-purple.svg)](https://opensource.org/licenses/MIT) [![

  • 0.0.14-7
  • npm
  • Socket score

Version published
Weekly downloads
7
decreased by-87.5%
Maintainers
1
Weekly downloads
 
Created
Source

BLoC React

codecov liscence Known Vulnerabilities Maintainability Rating

Reliability Rating Security Rating Vulnerabilities Bugs Code Smells

TypeScript BLoC pattern implementation for react using RxJS and heavily inspired by flutter_react - https://bloclibrary.dev

The BLoC Pattern (Business Logic Component) is a battle-tested design pattern for state management coming from Flutter and Dart. It tries to separate business logic from UI as much as possible while still being simple and flexible.

Everything revolves around subjects which are native to Dart, for JS there is a solid implementation by RxJS.

Quickstart

1. Create a new Bloc/Cubit

// CounterCubit.ts  
export default class CounterCubit extends Cubit<number> {  
 increment = (): void => { this.emit(this.state + 1); };}  

2. Create a new BlocReact instance and export useBloc from it

// state.ts  
const state = new BlocReact([new CounterCubit(0)]);  
export const { useBloc } = state;  

3. Use the hook to access the state and class methods

// CounterButton.tsx  
import { useBloc } from "../state";  
  
export default function CounterButton() {  
 const [count, { increment }] = useBloc(CounterCubit); return <button onClick={() => increment()}>count is: {count}</button>; }  

Documentation

[TODO]

BlocReact

The BlocReact class handles the global state and manages all communication between individual BLoCs. When initializing pass all the BLoCs for the global state in an array as first parameter.

const state = new BlocReact([new MyBloc(), new MyCubit()]);  

You can add an observer to all state changes global and local

state.observer = new BlocObserver({
  // onChange is called for all changes (Cubits and Blocs)
  onChange: (bloc, event) => console.log({bloc, event}),
  // onTransition is called only when Blocs transition from one state to another,
  // it is not called for Cubits
  onTransition: (bloc, event) => console.log({bloc, event}),
});

Cubit

A Cubit is a simplofied version Bloc class. Create your custom Cubit by extending the Cubit class, pass the initial state to the super constructor.

The Cubits' state is updated by calling the this.emit method with the new state.

export default class CounterCubit extends Cubit<number> {  
  constructor() {  
    super(0);  
  }  
  
  increment = (): void => {  
    this.emit(this.state + 1);  
  };  
  decrement = (): void => {  
    this.emit(this.state - 1);  
  };  
}

Bloc

Most of the time the Cubit class will be the easiest way to manage a peace of state but for the more critical parts of your application where there can be various reasons why the state changes to the same value, for example user authentication. It might be nice to know if the user got logged out because an error occured, the token expired or if they just clickd on the logout button. This is especially helpful when debugging some unexpected behaviour.

in the BlocObserver you can then use the onTransition to see why the state changes, it will pass the previous state, the event itself and the next state.

export enum AuthEvent {  
  unknown = "unknown",  
  authenticated = "authenticated",  
  unauthenticated = "unauthenticated",  
}  
  
export default class AuthBloc extends Bloc<AuthEvent, boolean> {  
  constructor() {  
    super(false)  
  
    this.mapEventToState = (event) => {  
      switch (event) {  
        case AuthEvent.unknown:  
          return false;  
        case AuthEvent.unauthenticated:  
          return false;  
        case AuthEvent.authenticated:  
          return true;  
    }
  };
}

In your app you can then update the state by "adding" an event. Use the useBloc hook to get access to the BLoC class and add an event.

const Component: FC = (): ReactElement => {  
  const [state, bloc] = useBloc<AuthBloc>(AuthBloc);  
  return <>  
    {state === true && <Button onClick={() => bloc.add(AuthEvent.unauthenticated)}>Logout</Button>}  
  </>  
}

useBloc

[TODO: Add docs for hook]

BlocProvider

[TODO: Add docs for hook alternative for class components]

BlocBuilder

[TODO: Add docs for local state]

FAQs

Package last updated on 08 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc