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
Redhooks is tiny React utility library for holding a predictable state container in your React apps.
4
-
Inspired by [Redux](https://redux.js.org), it reimplements redux concept by using the experimental Hooks API and the Context API.
5
-
4
+
Inspired by [Redux](https://redux.js.org), it reimplements the redux paradigm of state-management by using React's new Hooks and Context API, which have recently been [officially approved](https://github.com/facebook/react/pull/14679) by the React team.
6
5
-[Motivation](#motivation)
7
6
-[Basic Example](#basic-example)
8
7
-[Apply Middleware](#apply-middleware)
@@ -21,17 +20,17 @@ npm install --save redhooks
21
20
22
21
# Motivation
23
22
24
-
In the [Reactjs docs](https://reactjs.org/docs/hooks-custom.html) a nice paragraph titled useYourImagination() suggests to think on differents possible usages of the Hooks, this essentially is what Redhooks tries to do.
25
-
Redhooks does not use any third party library, it only depends on the new Hooks and the Context API.
26
-
You do not need to install `react-redux` to connect your components to the store because you can have access to it directly from any of your function components by calling `useStore` Redhooks api.
27
-
Hooks are not allowed within class Components, for using the store within them Redhooks exposes a HOC named `connect`.
28
-
It also supports the use of middlewares like `redux-thunk` or `redux-saga` or your custom middleware conforming to the middleware's API.
23
+
In the [Reactjs docs](https://reactjs.org/docs/hooks-custom.html) a nice paragraph titled _useYourImagination()_ suggests to think of different possible usages of functionality Hooks provide, which is essentially what Redhooks tries to do.
24
+
This package does not use any third party library, being only dependendent upon the Hooks and the Context API.
25
+
You do not need to install `react-redux` to connect your components to the store because you can have access to it directly from any of your function components by utilizing the `useStore` Redhooks API.
26
+
Hooks are [not allowed within class Components](https://reactjs.org/docs/hooks-rules.html), for using the store within them Redhooks exposes a Higher Order Component (HOC) named `connect`.
27
+
It also supports the use of middleware like `redux-thunk` or `redux-saga` or your own custom middleware conforming to the middleware's API.
29
28
30
29
# Basic Example
31
30
32
-
Redhooks follows the exact same principles of redux which is inspired to.
33
-
* the whole state of your app is stored in an object tree inside a single store.
34
-
* state is read only. So the only way to change the state is to dispatch an action, an object describing what happened.
31
+
Redhooks follows the exact same principles of redux, which was the package's inspiration.
32
+
* the total state of your app is stored in an object tree inside of a single store object.
33
+
* state is _read only_, so the only way to change the state is to dispatch an [action](https://redux.js.org/basics/actions), an object describing the changes to be made to the state.
35
34
* to specify how the actions transform the state tree, you write pure reducers.
## Dispatching Sync and Async Actions - No expensive rendering operation
102
-
If your component does not perform an expensive rendering you can use `useStore` Redhooks API within your
103
-
function Component in order to have access to the Redhooks store. Class or function components that perform expensive rendering operations can be connected to the store by using `connect` Redhooks HOC which takes care to avoid unnecessary re-rendering in order to improve the performance. But we will see it in action in the next paragraph.
100
+
## Dispatching Sync and Async Actions - Non-expensive rendering operation
101
+
If your component does not require expensive rendering, you can use the`useStore` Redhooks API within your
102
+
functional component in order to access the Redhooks store. Class or function components that perform expensive rendering operations can be connected to the store by using the `connect` Redhooks HOC which takes care to avoid unnecessary re-rendering in order to improve performance. We'll be able to see this in action below:
As for Redux, middleware is a way to extend Redhooks with custom functionality.
221
-
Middleware are functions which receive Store's `dispatch` and `getState` as named arguments, and return a function. Redhooks supports the use of the redux's middlewares like `redux-thunk`, `redux-saga` or you migth write a custom middleware conforming to middleware API.
219
+
As for Redux, [middleware](https://redux.js.org/advanced/middleware) is a way to extend Redhooks with custom functionality.
220
+
Middleware are functions which receive the store's `dispatch` and `getState` as named arguments, and subsequently return a function. Redhooks supports the use of redux middleware like `[redux-thunk](https://www.npmjs.com/package/redux-thunk)`, `[redux-saga](https://www.npmjs.com/package/redux-saga)` or you could write custom middleware to conform to the middleware API.
`createStore` returns the store object to be passed to the `<Provider store={store} />`.
372
-
* The `reducer` argument might be a single reducer function, a function returned by `combineReducers` or a plain object whose values are reducer functions if your store needs multiple reducers.
371
+
* The `reducer` argument might be a single reducer function, a function returned by `combineReducers` or a plain object whose values are reducer functions (if your store requires multiple reducers).
373
372
* The `opts` optional argument is an object which allows you to pass a `preloadedState`, `initialAction` and `middlewares`.
374
-
> The store is ready after the Provider is mounted, an `onload` event will be triggered at that time.
373
+
> The store is ready when the `Provider` is mounted, after which an `onload` event will be triggered.
375
374
376
375
#### Example
377
376
```js
@@ -401,10 +400,10 @@ const store = createStore(rootReducer)
401
400
```js
402
401
connect([mapStateToProps], [mapDispatchToProps])
403
402
```
404
-
`connect` function connects a React component to a Redhooks store. It returns a connected component class that wraps the component you passed in taking care to avoid unnecessary re-rendering. It should be used if your class or function components perform expensive rendering.
403
+
`connect` function connects a React component to a Redhooks store. It returns a connected component class that wraps the component you passed in taking care to avoid unnecessary re-rendering. It should be used if your class or function components require expensive rendering.
405
404
406
-
* If a `mapStateToProps` function is passed, your component will be subscribed to Redhooks store. Any time the store is updated, mapStateToProps will be called. The results of mapStateToProps must be a plain object, which will be merged into your component’s props. If you don't want to connect to Redhooks store, pass null or undefined in place of mapStateToProps.
407
-
*`mapDispatchToProps` if passed may be either a function that must return a plain object whose values are functions or a plain object whose values are [action creator](#action-creator) functions. In both cases the props of the returned object will be merged in your component’s props. If is not passed your component will receive `dispatch` prop by default.
405
+
* If a `mapStateToProps` function is passed, your component will be subscribed to Redhooks store. Any time the store is updated, `mapStateToProps` will be called. The results of `mapStateToProps` must be a plain object, which will be merged into your component’s props. If you don't want to connect to Redhooks store, pass null or undefined in place of mapStateToProps.
406
+
*`mapDispatchToProps`, if passed, may be either a function that returns a plain object whose values, themselves, are functions or a plain object whose values are [action creator](#action-creator) functions. In both cases the props of the returned object will be merged in your component’s props. If is not passed your component will receive `dispatch` prop by default.
0 commit comments