Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brunurd/clado

A React state machine based on Context API.
https://github.com/brunurd/clado

Last synced: 2 months ago
JSON representation

A React state machine based on Context API.

Awesome Lists containing this project

README

        

# Clado

[![git last tag][img-github-tag-badge]][link-github-tags] [![npm last version][img-npm-version-badge]][link-npm]

A React state machine based on Context API.

---

## Usage:

### StateMachine

The main component of `clado` is the `StateMachine`, where you can pass a bunch of components in the `states` using the state name as a key.

```jsx
import { StateMachine } from 'clado';

const App = () => {
return (

React State Machine


,
b: () => ,
}}
/>

);
};
```

### setState

To change the current state you can use the `setState` function of the `StateMachineContext`.

```jsx
import { useStateMachine } from 'clado';

const ComponentA = () => {
const { setState } = useStateMachine();

return (
<>

Component A


setState('b')}>Go To B
>
);
};
```

---

## Data Management

### Initial Data

The `StateMachine` accepts a data object to share data between the states.

```jsx
import { StateMachine } from 'clado';

const App = () => {
return (

React State Machine


,
b: () => ,
}}
/>

);
};
```

### Update Data

The way to update this data is using the `setState` function of the `StateMachineContext`. It only overrides the passed fields, any other will be preserved.

```jsx
const { setState } = useStateMachine();

setState('b', {
textValue: 'new text value',
})
}
>
Go To B
;
```

### Reading Data

Is possible to access this data using the `data` returned of the `StateMachineContext`.

```tsx
const { data } = useStateMachine<{ textValue: string }>();

{data.textValue}

;
```

Or in the states declaration as a parameter in the function.

```tsx
}
states={{
a: (data) => ,
b: (data) => ,
}}
/>
```

[img-github-tag-badge]:https://img.shields.io/github/v/tag/brunurd/clado?style=flat-square
[img-npm-version-badge]:https://img.shields.io/npm/v/clado/latest?style=flat-square
[link-github-tags]:https://github.com/brunurd/clado/tags
[link-npm]:https://www.npmjs.com/package/clado