Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/todor2810/use-reactive-state

useReactiveState() - a reactive alternative to React's useState()
https://github.com/todor2810/use-reactive-state

Last synced: about 1 month ago
JSON representation

useReactiveState() - a reactive alternative to React's useState()

Awesome Lists containing this project

README

        

`useReactiveState()` - a reactive alternative to React's `useState()`

---

## Installation

```
$ npm install use-reactive-state
```

## Example

useState()
useReactiveState()

```javascript
function Counter() {
const [state, setState] = useState({count: 0});

return (


You clicked {state.count}


{
setState({
...state,
count: state.count + 1
});
}}>
Click me


);
}
```

```javascript
function Counter() {
const state = useReactiveState({count: 0});

return (


You clicked {state.count} times


{ state.count += 1; }}>
Click me


);
}
```

## Limitation of `useReactiveState()`

- `state` cannot be destructured:

```javascript
// This won't work
const {count} = useReactiveState({count: 0});
```

```javascript
// This will work
const state = useReactiveState({count: 0});
```