You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: recipes/react/using-in-react-application/README.md
+79-1Lines changed: 79 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,4 +6,82 @@ If you are new to **simplux** there is [a recipe](../../basics/getting-started#r
6
6
7
7
> You can play with the code for this recipe in this [code sandbox](https://codesandbox.io/s/github/MrWolfZ/simplux/tree/master/recipes/react/using-in-react-application).
8
8
9
-
This recipe is still a work-in-progress.
9
+
Before we start let's install all the packages we need (we assume you already have all packages required for React installed).
10
+
11
+
```sh
12
+
npm i @simplux/core @simplux/immer @simplux/react @simplux/selectors redux -S
13
+
```
14
+
15
+
We also register all extension packages.
16
+
17
+
```ts
18
+
import'@simplux/immer'
19
+
import'@simplux/react'
20
+
import'@simplux/selectors'
21
+
```
22
+
23
+
Now we're ready to go.
24
+
25
+
In this recipe we are going to build a simple counter component. Let's start by creating our module with some simple mutations and selectors. The React extension package adds a new `react` property to our modules that provides us a with a React hook called `useSelector` for using a module's state in a component.
26
+
27
+
```ts
28
+
const {
29
+
createMutations,
30
+
createSelectors,
31
+
32
+
// the simplux react extension adds a hook for using the module's
Now we can start using this module in our counter component. As the name suggests the `useSelector` hook allows us to use our module's selectors inside our component. This hook also ensures that the component is updated whenever the selected value changes.
60
+
61
+
```tsx
62
+
const Counter = () => {
63
+
const value =useSelector(selectCounterValue)
64
+
65
+
// we can also use an inline selector
66
+
const valueTimesTwo =useSelector(s=>s.value*2)
67
+
68
+
return (
69
+
<>
70
+
value: {value}
71
+
<br />
72
+
value * 2: {valueTimesTwo}
73
+
<br />
74
+
{/* we can use mutations directly as event handlers */}
75
+
<buttononClick={increment}>Increment</button>
76
+
{/* we can also use mutations with arguments */}
77
+
<buttononClick={() =>incrementBy(5)}>Increment by 5</button>
78
+
</>
79
+
)
80
+
}
81
+
```
82
+
83
+
And that is all you need to use **simplux** in your React application.
84
+
85
+
If your application also uses Redux we recommend you take a look at [our recipe](../../advanced/using-in-redux-application#readme) for using **simplux** with a custom Redux store.
86
+
87
+
We also encourage you to learn about [how to test](../testing-components-using-state#readme) the component that we have just created.
0 commit comments