# react-stream-bloc

> React Stream Bloc

Latest version **3.1.1** (published 2026-01-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-stream-bloc
pnpm add react-stream-bloc
yarn add react-stream-bloc
bun add react-stream-bloc
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2026-01-26 |
| First published | 2023-10-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Blencm |
| Maintainers | blencm |
| Keywords | react, bloc, builder, stream, react-stream, react-builder |

## Links

- npm: https://www.npmjs.com/package/react-stream-bloc
- Repository: https://github.com/blencm/react-stream-bloc
- Homepage: https://github.com/blencm/react-stream-bloc#readme
- Issues: https://github.com/blencm/react-stream-bloc/issues
- npm.io page: https://npm.io/package/react-stream-bloc

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) ^7.8.2
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.2.0
- [prop-types](https://npm.io/package/prop-types.md) ^15.8.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.1.1 (latest) — 2026-01-26
- 3.1.0 — 2025-02-26
- 3.0.3 — 2025-02-26
- 3.0.2 — 2024-01-30
- 3.0.1-rm.1 — 2024-01-20
- 3.0.1 — 2024-01-20
- 3.0.0 — 2024-01-20
- 2.0.9 — 2024-01-20
- 2.0.8 — 2024-01-19
- 2.0.7 — 2024-01-19
- 2.0.6 — 2024-01-19
- 2.0.5 — 2024-01-19
- 2.0.3 — 2024-01-19
- 2.0.2 — 2024-01-19
- 2.0.1 — 2024-01-19
- … 11 more at https://npm.io/package/react-stream-bloc/versions

## README

# `react-stream-bloc`

The `react-stream-bloc` package contains only the functionality necessary to define Bloc models in JavaScript and TypeScript. Typically used in user interface components.

[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://opensource.org/licenses/MIT)

## Install

Using npm:

```sh
npm install react-stream-bloc
```

## Documentation

The basic idea behind the Bloc pattern is simple. Business logic will be isolated from UI components. First, they will use an observer to send events to the Bloc. The Bloc will then notify UI components via observables after processing the request.

### Adaptability for updating the application's logic

The impact on the application is minimal when the business logic is separated from the UI components. You may alter the business logic whenever you want without impacting the UI components.

### Repurpose logic

Because the business logic is in one location, UI components may reuse logic without duplicating code, increasing the app's simplicity.

### Scalability

Application needs may evolve over time, and business logic may expand. In such cases, developers can even construct many BloCs to keep the codebase clear.

## Usage example

<a href="https://codesandbox.io/s/react-stream-bloc-example-qk3kjy"><img src="https://uploads.codesandbox.io/uploads/user/193b58fe-97f6-4cde-9078-6bb2dc49b95d/WT71-CodeSandbox.png"></a>

Create a state file and add your variables as you like.

```javascript
export interface User {
  id: string;
  name: string;
}

export type UserState = {
  loading: boolean,
  adding?: boolean,
  data: User[],
  message?: string,
};

export const UserInitialState: UserState = {
  loading: false,
  data: [],
};
```

Create a Bloc class file and add your functions, to change state always add `this.changeState(...)`

```javascript
import { Bloc } from "react-stream-bloc";
import axios from "axios";

axios.defaults.baseURL = 'http://localhost:4000/api/';

export class UserBloc extends Bloc<UserState> {
  private users: User[] = [];

  constructor() {
    super(UserInitialState);
  }

  async getUsers() {
    this.changeState(...this.state, loading: true);

    await axios.get("users")
      .then((response) => {
        this.changeState(this.mapToLoadedState(response.data));
      })
      .catch((err) => {
        this.changeState(...this.state, loading: false, message: err.toString());
      });
  }

  async addUser(data: User){
    this.changeState(...this.state, adding: true);
        await axios.post("user", data)
      .then((response) => {
        if(response.status === 200){
          const user = response.data as User;
          const newUsers = [user].concat(this.users);
          this.changeState(this.mapToLoadedState(newUsers));
        }else{
          this.changeState(...this.state, adding: false);
        }
      })
      .catch((err) => {
        this.changeState(...this.state, adding: false, message: err.toString());
      });
  }

  mapToLoadedState(users: User[]): UserState {
    this.users = users;
    return {
      loading: false,
      adding: false,
      data: this.users,
    };
  }
}
```

Create a dependency provider file, here you initialize the data when your application is opened. If you want to extend the data initialization of a class or function, Easily insert it into your Bloc file.

```javascript
export function provideUserBloc() {
  const bloc = new UserBloc();
  return bloc;
}
```

Create a provider file.

```javascript
import { createContext } from "react-stream-bloc";

const [userContext, useUser] = createContext<UserBloc>();

const useUserBloc = useUser;
export { userContext, useUserBloc };
```

### Implementing UI components.

This file is a child that will be listening or sending values using the Bloc, all children that are listening will update the data sent or received.

```javascript
import { BlocBuilder } from "react-stream-bloc";

export const UserContent = () => {
  const bloc = useUserBloc();

  const handleAdd = () => {
    const user: User = {
      id: "id",
      name: "Stream Bloc",
    };
    bloc.addUser(user);
  };

  return (
    <div style={{ margin: 30 }}>
      <BlocBuilder
        bloc={bloc}
        builder={(state: UserState) => (
          <div>
            <button onClick={state.adding ? null : handleAdd}>
              {state.adding ? "Loading..." : "Add"}
            </button>
            {state.loading ? <div> Loading... </div> : <div> {content} </div>}
          </div>
        )}
      />
    </div>
  );
};
```

In App add BlocProvider, you can add multiple providers in a list and initialize the value of DependenciesProvider.

```javascript
import { BlocProvider } from "react-stream-bloc";
import { userContext } from "./UserProvider";

const App = () => {
  return (
    <BlocProvider
      providers={[<userContext.Provider value={providerUserBloc()} />]}
    >
      <UserContent />
    </BlocProvider>
  );
};

export default App;
```

## Authors

- [@blencm](https://www.github.com/blencm)

---
_Source: https://npm.io/package/react-stream-bloc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
