hops-redux
v15.2.1
Published
React and Redux implementation for Hops
Downloads
292
Readme
hops-redux
Please see the main Hops Readme for general information and a Getting Started Guide.
This is a preset for Hops that can be used to add redux support to your Hops project.
Installation
This preset must be used together with the hops-react
preset.
Add this preset and its peer dependencies react-redux
, redux
and redux-thunk
to your existing Hops React project:
npm install --save hops-redux react-redux redux redux-thunk
If you don't already have an existing Hops project read this section on how to set up your first Hops project.
Usage
In order to use Redux in your application install the plugin and configure your reducers via render options.
Check out this integration test as an example for how to use this preset.
Configuration
Preset Options
| Name | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| allowServerSideDataFetching
| Boolean
| true
| no | Whether Hops is allowed to execute route-bound action creators during server-side rendering |
allowServerSideDataFetching
If you don't want Hops to execute route-bound action creators during server-side rendering, set this value to false
.
Bear in mind, that setting this value to true
on the other hand has no mandatory character. This means that there's no way to force Hops to execute server-side requests. As soon as there's a single Hops preset in place, that either sets the allowServerSideDataFetching
-value to false
or implements the canPrefetchOnServer
-hook to return false
, there won't be any server-side requests.
Render Options
This preset has only a single runtime option which can be passed to the render()
options inside the redux
key (see example above).
| Name | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| redux.reducers
| Object
| {}
| yes | An object whose values consists of all your reducer functions. |
| redux.middlewares
| Array
| [ReduxThunkMiddleware]
| no | An array of all redux middleware you want to use. |
| redux.actionCreators
| Array
| []
| no | An array of route-bound action creators to be dispatched when the current route matches. |
| redux.alwaysDispatchActionsOnClient
| boolean
| undefined
| no | When using server side rendering the route-matching actions will be dispatched on the server only - pass true
to also dispatch these actions on the client again. |
reducers
An object with key/value pairs of namespaces and reducer functions which will shape your state tree. This will be used with the combineReducers
function.
const reducers = {
counter: function (state, action) {
return action.type === 'increment' ? state + action.payload : state;
},
};
export default render(<MyApp />, { redux: { reducers } });
middlewares
You can configure any redux middleware you may want to use - by default this preset will include the redux-thunk
middleware.
import logger from 'redux-logger';
import thunk from 'redux-thunk';
export default render(<MyApp />, { redux: { middlewares: [logger, thunk] } });
actionCreators
In order to dispatch actions based on the currently matching route you can specify a list of actions for matching urls.
These objects have the same properties as the <Route />
component and an additional action
key with which the action that is to be dispatched can be specified.
When server-side rendering/data fetching is enabled, this will dispatch matching actions on the server and prefill the store for client-side.
On the client-side by default this will dispatch matching actions only on client-side navigation (can be overridden by setting alwaysDispatchActionsOnClient
to true
).
Actions receive two parameters: params
(see URL Parameters in the react-router docs) and an object containing location
(the react router location
object) and [match
]((https://reacttraining.com/react-router/web/api/match).
export default render(<MyApp />, {
redux: {
actionCreators: [
{
path: '/users',
exact: true,
strict: true,
action: fetchUsers,
},
{
path: '/users/:id',
action: ({ id }) => fetchUser(id),
},
],
},
});
alwaysDispatchActionsOnClient
Use this option to control whether you want to dispatch matching actions on the client-side again after they have already been dispatched on the server.
export default render(<MyApp />, {
redux: {
actionCreators: [...],
alwaysDispatchActionsOnClient: true,
},
});
Mixin Hooks API
Caution: Please be aware that the mixin hooks are not part of the SemVer API contract. This means that hook methods and signatures can change even in minor releases. Therefore it's up to you to make sure that all hooks that you are using in your own mixins still adhere to the new implementation after an upgrade of a Hops packages.
getReduxStore(): Store
(override) runtime/browser/server
Use this method in your own mixins to get a reference to the currently used Redux Store instance.
getReduxMiddlewares(): [middleware]
(override) runtime/browser/server
Allows to specify your own set of redux middlewares. Useful when middlewares need access to the current request object, which only exists in the mixin context. Passes fetch implementation as extra argument to thunks.
Beware that middlewares passed as render option take precedence.
Example thunk-based action creator
// Object with fetch is passed as third parameter to thunks
const incrementFetch =
() =>
(dispatch, getState, { fetch }) => {
return fetch('/api')
.then((r) => r.json())
.then(({ value }) => {
dispatch({ type: 'INCREMENT', payload: value });
});
};
canPrefetchOnServer(): boolean
(sequence) server
This is a hook that can be used to customize the behavior of when Hops can prefetch data during server-side rendering. E.g. execute route-bound action creators during initial render. If any function of this sequence returns false it prevents server fetching for this request.
By default it returns whatever is configured in the allowServerSideDataFetching
preset option.
In case you need more control over the server-side rendering you can implement this method and provide your own implementation that decides if data should be prefetched during server-side rendering.