Skip to content

Commit

Permalink
simplify peer deps
Browse files Browse the repository at this point in the history
  • Loading branch information
charkour committed Jun 1, 2024
1 parent 94db295 commit b1159ff
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ dependency injection for [zustand](https://github.com/pmndrs/zustand) with [reac
npm install react zustand zustand-di
```

> Note: `zustand-di` requires `react` and `zustand` as peer dependencies.
> For `[email protected]` and higher, you need `[email protected]` or higher.
> For `zustand@~4.0.0`, you need `[email protected]` or lower.
## Usage

```tsx
Expand All @@ -37,8 +41,12 @@ const createStore = (initialState: { count: number }) =>
// 3. Use the store in a child component
function Counter() {
const { count, inc } = useStore((state) => state);
useEffect(inc, [inc]);
return <div>count: {count * 1}</div>;
return (
<>
<button onClick={inc}>increment</button>
<div>count: {count}</div>
<>
);
}

// 4. Use the store in the app and pass in the store creator
Expand All @@ -63,33 +71,11 @@ This package was originally inspired by [`createContext`](https://github.com/pmn

For a detailed explanation, check out TkDoDo's [blog post](https://tkdodo.eu/blog/zustand-and-react-context).

### Why is this useful?
### Why is Dependency Injection useful within React?

- **Dependency Injection**: You can pass in props to the store creator.
- This is useful for testing and [initializing the store with props](https://github.com/pmndrs/zustand/blob/main/docs/guides/initialize-state-with-props.md).
- You can also use this to create multiple instances of the same store with different props.
- **Type Safety**: You can define the store type and use it in the `useStore` hook.
- **Simplified API**: The API is simplified and easier to use than the original `zustand/context`.

## Peer Dependencies

For `[email protected]` and higher

```json
{
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"zustand": "^4.1.0"
}
```

For `[email protected]` and below

```json
{
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"zustand": "^4.0.0"
}
```

## API

Expand Down

0 comments on commit b1159ff

Please sign in to comment.