npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@bloc-arch/react

v1.2.1

Published

Provides components and utils to use BLoC architecture in React.

Readme

@bloc-arch/react

https://travis-ci.com/kasperski95/bloc-arch--react.svg?branch=master

Package provides hooks and components for BLoC architecture in React.

Table of Contents


Related packages

Setup and Usage Example (TypeScript)

// src/index.tsx
// ...
import { BlocProvider } from './blocs/setup-blocs'

ReactDOM.render(
  <React.StrictMode>
    <BlocProvider> {/* <= */}
      <App />
    </BlocProvider>
  </React.StrictMode>,
  document.getElementById('root')
)
// src/blocs/setup-blocs.ts
// ...
import { setupBlocs } from '@bloc-arch/react'

export const { BlocProvider, useWeakBloc, useBloc } = setupBlocs({
  sample: (dependency: any) => new SampleBloc(dependency),
  // ...
})
// src/blocs/sample/SampleBloc.ts
// (generated by @bloc-arch/cli)

import { Bloc } from '@bloc-arch/core'
import * as SampleEvents from './SampleEvent'
import * as SampleStates from './SampleState'

export class SampleBloc extends Bloc<
  SampleEvents.SampleEvent,
  SampleStates.SampleState
> {
  constructor() {
    super(new SampleStates.Initial())
  }

  async *mapEventToState(event: SampleEvents.SampleEvent) {
    // if (event instanceof SampleEvents.Foo) {
      yield new SampleStates.Loading()
      await new Promise((resolve) => setTimeout(resolve, 2000)) // wait 2s
      yield new SampleStates.NotInitial()
    /* 
    } else if (event instanceof SampleEvents.Bar) {
      ...
    }
    */
  }
}
// src/App.tsx
// ...
import { BlocBuilder, useBlocRenderProp } from '@bloc-arch/react'
import { useBloc } from './blocs/setup-blocs'

function App() {
  const sampleBloc = useBloc('sample')('dependency')

  return (
    <>
      <button
        onClick={() => {
          sampleBloc.dispatch(new SampleEvents.Foo())
        }}
      >
        click
      </button>
      <BlocBuilder
        bloc={sampleBloc}
        renderer={useBlocRenderProp((state) => {
          if (state instanceof SampleStates.Loading) {
            return <div>loading state</div>
          } else if (state instanceof SampleStates.NotInitial) {
            return <div>not initial state</div>
          }
          return <div>initial state</div>
        }, [])}
      />
    </>
  )
}

Docs

setupBloc

Takes as an argument object with functions creating blocs and returns BlocProvider, useBloc and useWeakBloc.

// setup blocs:
const { BlocProvider, useBloc, useWeakBloc } = setupBlocs({
  sample: () => new SampleBloc()
})

useBloc

Returns a function to instantiate bloc. Bloc is created only if it doesn't exist. Disposes bloc on component unmount.

// setup blocs:
export const { BlocProvider, useBloc } = setupBlocs({
  //...
  sample: () => new SampleBloc(),
})

// component:
const bloc = useBloc("sample")()

useWeakBloc

Returns a function to get a specified bloc if is available. Bloc is available between the time of creation and disposal.

// component:
const getSampleBloc = useWeakBloc("sample")
// ...
const handleClick = React.useCallback(() => {
  getSampleBloc()?.dispatch(new SampleEvents.Foo())
}, [getSampleBloc])

BlocProvider

Provider of blocs. Use property blocFactories to provide mocked blocks in case of testing.

// root component:
<BlocProvider blocFactories={{ sample: (dependency: any) => new SampleBlocMock() }}>
...
</BlocProvider>

BlocBuilder, useBlocRenderProp

BlocBuilder subscribes to bloc and maps states to views by the use of property renderer. Property listener is optional. Use it to react on state changes.

"useBlocRenderProp" is a pretty much a React.useCallback with ability to deduce type of state (TypeScript).

// component:
<BlocBuilder
  bloc={sampleBloc}
  listener={React.useCallback((state: SampleStates.SampleState) => {
    if (state instanceof SampleStates.NotInitial) {
      props.callback()
    }
  }, [props.callback])}
  renderer={useBlocRenderProp((state) => {
    if (state instanceof SampleStates.Loading) {
      return <div>loading state</div>
    } else if (state instanceof SampleStates.NotInitial) {
      return <div>not initial state</div>
    } else {
      return <div>{props.someProp}</div>
    }
  }, [props.someProp])}
/>