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: README.md
+36-18Lines changed: 36 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,10 +43,6 @@ Convert hooks into shared states between react components
43
43
-[Dark Mode][expo-app] with React Native on expo. Project in [`example/`](https://github.com/react-native-toolkit/rex-state/tree/master/example) directory
44
44
- ✨ [Why Rex State?](#why-rex-state)
45
45
46
-
## Motivation
47
-
48
-
Rex State was initially built as a state management library. But after using it in many projects, its main purpose became creating hooks where the data returned by the hook can be shared across multiple react components.
49
-
50
46
## Requirements
51
47
52
48
Rex State is built purely on React Hooks hence it requires React > 16.8 to work.
@@ -63,35 +59,51 @@ npm i rex-state
63
59
64
60
## Usage
65
61
66
-
```jsx
67
-
import { createRexStore } from'rex-state';
62
+
Consider the following hook which lets you toggle theme between light & dark modes
68
63
69
-
// A simple hook to toggle theme modes between 'light' & 'dark'
The `useStore` hook & `RexProvider` are renamed to `useTheme` & `ThemeProvider` for use in the application.
85
+
86
+
Now you can wrap your entire Application inside the `ThemeProvider` to ensure the context is setup properly for the `useTheme` hook.
82
87
83
-
// Use the `ThemeProvider` at the top level of your React component tree
88
+
```jsx
84
89
constApp= () => {
85
-
// `initialTheme` value can be supplied using the value prop of `ThemeProvider`
86
90
return (
87
91
<ThemeProvider value="dark">
88
92
{/* Rest of your application */}
89
93
<ToggleButton />
94
+
<ThemeText />
90
95
</ThemeProvider>
91
96
);
92
97
};
98
+
```
99
+
100
+
> Note: The value of the argument of `useThemeMode` function - `initialTheme` is supplied to the `ThemeProvider` using the `value` prop. The `value` prop only supports a single argument. Hence if your hook requires multiple arguments, you can pass them as a single object
93
101
94
-
// All components can now access the `useTheme` hook
102
+
Once you add the `ThemeProvider` to the top of your application's tree, the child components can now use the `useTheme` hook to access the result of your `useThemeMode` hook. This time, when you call `toggleTheme` in any of the child components, it will cause your entire application tree to re-render & all the components that use the `useTheme` hook will have the updated value!
103
+
104
+
The following is a toggle button that toggles the theme when users click on it.
105
+
106
+
```jsx
95
107
constToggleButton= () => {
96
108
const [theme, toggleTheme] =useTheme();
97
109
@@ -102,9 +114,11 @@ const ToggleButton = () => {
102
114
</View>
103
115
);
104
116
};
117
+
```
118
+
119
+
The next component is a text block that simply displays the theme's mode
105
120
106
-
// Calling `toggleTheme` in above component will cause updates
107
-
// in all the components in the Application tree using the context API
121
+
```jsx
108
122
constThemeText= () => {
109
123
const [theme] =useTheme();
110
124
@@ -117,11 +131,15 @@ const ThemeText = () => {
117
131
};
118
132
```
119
133
134
+
Invoking the `toggleTheme` function from the `<ToggleButton/>` component updates the `<ThemeText/>` component. This means your hook is now a shared state between the two components!
135
+
136
+
Also, check out the [counter example](https://codesandbox.io/s/rex-counter-2m4zy?file=/src/App.js) from codesandbox
137
+
138
+
Rex State is good for some use cases and not suitable for some use cases since it uses the [React Context](https://reactjs.org/docs/context.html#api) API which is considered inefficient as a change causes the entire React child tree to re-render. Read the [performance](https://rex-state.netlify.app/?path=/story/intro-performance--page) section to see how to use Rex State effectively.
139
+
120
140
## Why Rex State?
121
141
122
-
- It's Tiny!
123
-
- Simple & un-opinionated
124
-
- Makes hooks much more powerful
142
+
Rex State is a handy utility to make your hooks more powerful. It is simple, un-opinionated & is very tiny!
The `useStore` hook & `RexProvider` are renamed to `useTheme` & `ThemeProvider` for use in the application.
64
+
65
+
Now you can wrap your entire Application inside the `ThemeProvider` to ensure the context is setup properly for the `useTheme` hook.
55
66
56
-
// Use the `ThemeProvider` at the top level of your React component tree
67
+
```jsx
57
68
constApp= () => {
58
-
// `initialTheme` value can be supplied using the value prop of `ThemeProvider`
59
69
return (
60
70
<ThemeProvider value="dark">
61
71
{/* Rest of your application */}
62
72
<ToggleButton />
73
+
<ThemeText />
63
74
</ThemeProvider>
64
75
);
65
76
};
77
+
```
78
+
79
+
> Note: The value of the argument of `useThemeMode` function - `initialTheme` is supplied to the `ThemeProvider` using the `value` prop. The `value` prop only supports a single argument. Hence if your hook requires multiple arguments, you can pass them as a single object
66
80
67
-
// All components can now access the `useTheme` hook
81
+
Once you add the `ThemeProvider` to the top of your application's tree, the child components can now use the `useTheme` hook to access the result of your `useThemeMode` hook. This time, when you call `toggleTheme` in any of the child components, it will cause your entire application tree to re-render & all the components that use the `useTheme` hook will have the updated value!
82
+
83
+
The following is a toggle button that toggles the theme when users click on it.
84
+
85
+
```jsx
68
86
constToggleButton= () => {
69
87
const [theme, toggleTheme] =useTheme();
70
88
@@ -75,9 +93,11 @@ const ToggleButton = () => {
75
93
</View>
76
94
);
77
95
};
96
+
```
78
97
79
-
// Calling `toggleTheme` in above component will cause updates
80
-
// in all the components in the Application tree using the context API
98
+
The next component is a text block that simply displays the theme's mode
99
+
100
+
```jsx
81
101
constThemeText= () => {
82
102
const [theme] =useTheme();
83
103
@@ -90,13 +110,25 @@ const ThemeText = () => {
90
110
};
91
111
```
92
112
93
-
## Working Example
113
+
## In Action
114
+
115
+
<br />
94
116
95
117
<DarkModeProvider value="dark">
96
118
<ToggleButton />
97
119
<ThemeText />
98
120
</DarkModeProvider>
99
121
122
+
<br />
123
+
<br />
124
+
<br />
125
+
126
+
As you can see, calling the `toggleTheme` function from the `<ToggleButton/>` component updates the `<ThemeText/>` component. This means your hook is now a shared state between the two components!
127
+
128
+
Also, check out the [counter example](https://codesandbox.io/s/rex-counter-2m4zy?file=/src/App.js) from codesandbox
129
+
130
+
Rex State is good for some use cases and not suitable for some use cases since it uses the [React Context](https://reactjs.org/docs/context.html#api) API which is considered inefficient as a change causes the entire React child tree to re-render. Read the performance section to see how to use Rex State effectively.
Copy file name to clipboardExpand all lines: example/src/stories/Performance.stories.mdx
+12-2Lines changed: 12 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,16 @@ import { Meta } from '@storybook/addon-docs/blocks';
6
6
7
7
Rex State makes the values returned by Hooks shareable using the React Context API. But there are knows performance limitations since changing the value of Context causes the entire Application tree to re-render.
8
8
9
-
Rex State used to have an [`InjectStore`](https://github.com/daniakash/rex-state/tree/9809c7a7a6f71c1644a7d94a058b5606cf49da11#performance) HOC which implemented the solutions suggested by [Dan Abramov](https://github.com/facebook/react/issues/15156#issuecomment-474590693). This HOC is removed in v1.0 as the goal of Rex State is no longer to be a state management tool, but, to be an utility to share hooks across components.
9
+
## When not to use Rex State
10
10
11
-
You can directly implement the performance optimizations suggested in the above comment if you need. If you are looking for a powerful state management tool that can efficiently avoid unnecessary re-renders, you can try [recoil](https://recoiljs.org/) or [zustand](https://github.com/react-spring/zustand).
11
+
Rex State is not a state management library. It is a utility to quickly share a hook between two components. Hence if you are working with data that changes often and is shared by a large number of components, it is a good idea to avoid rex state.
12
+
13
+
Rex State used to have an [`InjectStore`](https://github.com/daniakash/rex-state/tree/9809c7a7a6f71c1644a7d94a058b5606cf49da11#performance) HOC which implemented the solutions suggested by [Dan Abramov](https://github.com/facebook/react/issues/15156#issuecomment-474590693). This HOC is removed in v1.0 as the goal of Rex State is no longer to be a state management tool. You can directly implement the performance optimizations suggested in [the comment](https://github.com/facebook/react/issues/15156#issuecomment-474590693) if you need.
14
+
15
+
If you are looking for a powerful state management tool that can efficiently avoid unnecessary re-renders, you can try [recoil](https://recoiljs.org/) or [zustand](https://github.com/react-spring/zustand).
16
+
17
+
## When you can use Rex State
18
+
19
+
Rex State is good for data that doesn't change often. For example, managing color schemes of dark mode & light mode throughout your application is a good use case. As the theme modes do not change often. Also, changing the mode usually requires all the components to re-render.
20
+
21
+
If you have data that changes often but you want to share the data of your hook with a limited number of components alone, then instead of wrapping your entire application inside the `RexProvider`, you can only wrap the nearest common parent of the required components inside the `RexProvider`. This ensures the re-renders are limited only to that parent and the rest of your application is not disturbed.
0 commit comments