Skip to content

Commit 9494a8b

Browse files
committed
🛳 Code shipped !!
1 parent 48e7912 commit 9494a8b

13 files changed

+7338
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.next
2+
node_modules
3+
*.log

README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
# redux-multiple-states-nextjs
1+
# Multiple Redux States in single NodeJS web application
2+
3+
## Description
24
A prototype built with React, Redux, and NextJS (for routing) that shows how one can do CRUD to multiple state trees within a single webapp. Uses fast-redux, now you don't need to use constants for action types to match actions with reducers, as compared to conventional redux.
5+
6+
## Setup
7+
8+
Install NPM modules:
9+
`npm install`
10+
11+
## Running
12+
13+
To run in development mode that supports hot reloading:
14+
`npm run dev`
15+
16+
To run in production mode:
17+
`npm start`
18+
19+
## Analyse how the modules are bundled
20+
21+
Run:
22+
`npm run analyze`

config/redux.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { createStore, applyMiddleware } from 'redux'
2+
import { composeWithDevTools } from 'redux-devtools-extension'
3+
import thunkMiddleware from 'redux-thunk'
4+
import withRedux from 'next-redux-wrapper'
5+
import { rootReducer } from 'fast-redux'
6+
7+
export const initStore = (initialState = {}) => {
8+
return createStore(rootReducer, initialState,
9+
composeWithDevTools(applyMiddleware(thunkMiddleware)))
10+
}
11+
12+
export const reduxPage = (comp) => withRedux(initStore)(comp)

containers/about.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// NextJs Stuff
2+
import Link from 'next/link'
3+
4+
// Redux stuff required for later
5+
import {bindActionCreators} from 'redux'
6+
import {connect} from 'react-redux'
7+
8+
// Import in your state(s)
9+
import { getHomepageState, bumpBuild } from '../states/treeOne'
10+
import { getAboutpageState, bumpVersion } from '../states/treeTwo'
11+
12+
// HTML / JSX Code:
13+
const About = ({ version, gender, build, bumpVersion }) => (
14+
<div>
15+
<h1>About Us</h1>
16+
<h3>Current VERSION: {version}</h3>
17+
<h3>Current BUILD: {build}</h3>
18+
<h2>hello {gender}</h2>
19+
<p><button onClick={(e) => bumpVersion(1)}>Bump version only!</button></p>
20+
<Link href='/'><a>Homepage</a></Link>
21+
</div>
22+
)
23+
24+
// Mapping State Variables and Actions to Props:
25+
function mapStateToProps(state) {
26+
let stateOne = getHomepageState(state)
27+
let stateTwo= getAboutpageState(state, 'version', 'gender')
28+
let combinedState = { ...stateOne, ...stateTwo} // This is the part where we can merge the two states and use it as a combined state
29+
return combinedState
30+
}
31+
function mapDispatchToProps (dispatch) {
32+
return bindActionCreators({ bumpVersion }, dispatch)
33+
}
34+
35+
// Export Container to be used in Pages:
36+
export default connect(mapStateToProps, mapDispatchToProps)(About)

containers/homepage.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// NextJs Stuff
2+
import Link from 'next/link'
3+
4+
// Redux stuff required for later
5+
import {bindActionCreators} from 'redux'
6+
import {connect} from 'react-redux'
7+
8+
// Import in only one state tree this time
9+
import { getHomepageState, bumpBuild } from '../states/treeOne'
10+
11+
// HTMl / JSX code:
12+
const Homepage = ({ build, bumpBuild }) => (
13+
<div>
14+
<h1>Homepage</h1>
15+
<h3>CURRENT build: {build}</h3>
16+
<p><button onClick={(e) => bumpBuild(1)}>Bump build!</button></p>
17+
<Link href='/about'><a>About Us</a></Link>
18+
</div>
19+
)
20+
21+
// Mapping State Variables and Actions to Props:
22+
function mapStateToProps (state) {
23+
return getHomepageState(state)
24+
}
25+
function mapDispatchToProps (dispatch) {
26+
return bindActionCreators({ bumpBuild }, dispatch)
27+
}
28+
29+
// Export Container to be used in Pages:
30+
export default connect(mapStateToProps, mapDispatchToProps)(Homepage)

next.config.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This is for running an analyser for development purposes.
2+
// To use this, run 'npm run analyse' in your terminal.
3+
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
4+
const { ANALYZE } = process.env
5+
6+
module.exports = {
7+
webpack: function (config) {
8+
if (ANALYZE) {
9+
config.plugins.push(new BundleAnalyzerPlugin({
10+
analyzerMode: 'server',
11+
analyzerPort: 8888,
12+
openAnalyzer: true
13+
}))
14+
}
15+
16+
return config
17+
}
18+
}

0 commit comments

Comments
 (0)