A redux delegate object used to create store, render page, and integrate React-Router and more.
It's really popular to integrate React with React-Router, Redux and related development tools.
Most likely, we'll need install the following packages:
yarn add react
yarn add react-dom
yarn add react-redux
yarn add react-router
yarn add react-router-dom
yarn add react-router-redux
yarn add redux
yarn add redux-thunk
yarn add -D react-hot-loader
yarn add -D redux-devtools
yarn add -D redux-devtools-dock-monitor
yarn add -D redux-devtools-log-monitor
yarn add -D redux-logger
And do a lot of work to integrate them well. It's so boring and complicated for me, especially there are several React projects need to be maintained.
This package extracts these works from my project, and provides a better API to accomplish all related works.
In your React app, install the following packages:
yarn add react
yarn add react-dom
yarn add redux-context
yarn add -D react-hot-loader
yarn add -D redux-devtools-extension
yarn add -D redux-logger
redux-devtools-extension
is built on the top of redux-devtools
, and integrates several useful tools.
redux-logger
will print the information of dispatched action of Redux. You may remove it if you don't like it.
In entry JavaScript, we could use redux-context
to create history which is used by React-Router, initial Redux store, and render React component to DOM element.
import React from 'react';
import ReduxContext from 'redux-context';
import MyApp from './MyApp';
import reducers from './reducers';
let domElement = document.getElementById('page');
let context = ReduxContext
.forDOM(domElement)
.configureStore(reducers)
.render(MyApp);
if (module.hot) {
module.hot.accept('./reducers', () => {
context.replaceReducer(require('./reducers').default);
});
module.hot.accept('./MyApp', () => {
context.render(MyApp);
});
}
- If
domElement
is a DOMElement
object, it will be used directly. - If
domElement
isString
object, it will be used to find the Element viadocument.querySelector()
. - If
domElement
is omitted,redux-context
will try to find DOM Element which id ispage
orroot
.
If a valid domElement
is existed, redux-context
will extract data-*
attributes from it, and merge them with window.pageProps
.
let defaultProps = Object.assign({}, dataAttrs, window.pageProps)
The defaultProps
will be send to ReactElement
which is used in render()
as props.
creator
is a callback function, used to createhistory
object. It will receivedefaultProps
as argument.
In fact, there is no need to call createHistory()
manually. redux-context
will create a browserHistory
for you as default.
reducers
is a plain object, which contains several reducer functionsinitialState
is optional
ReactElement
is a class or instance of React Componentprops
will be merged withdefaultProps
, and injectstore
andhistory
reducers
is a plain object, which contains several reducer functions
As result, redux-context
will generate different following React Component trees for production mode and development mode.
<withRedux>
<Provider>
<ConnectedRouter>
<Router>
<MyApp />
</Router>
</ConnectedRouter>
</Provider>
</withRedux>
<AppContainer>
<withRedux>
<Provider>
<ConnectedRouter>
<Router>
<MyApp />
</Router>
</ConnectedRouter>
</Provider>
</withRedux>
</AppContainer>
- For Chrome
- from Chrome Web Store
- For FireFox
- from Mozilla Add-ons
For more information, you may check the page of redux-devtools-extension.