
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
redux-shape
Advanced tools
A simple util for generating redux action & reducer by a state shape definition.
Feeling tedious to write redudx action and reducer in seperate files?
Not sure what is the state shape created by your reducers?
Don't know what action type deals with what reducer?
redux-shape make it easy to create a state shape by a shape definition. With the shape defined, your action type and reducer will be determined.
npm install --save redux-shape
redux-shape has two concepts.
leaf defines your leaf node of the shape and it's where you write your reducers and state on which the reducers work.
let leaf = {
state:"",
reducers:{
changeText(state,action){
let text = action.text
return text;
},
clearText(state,action){
return '';
}
}
}
shape defines your state shape with leaf.
let shape = {
text:()=>leaf,// a leaf should be returned inside a function.
}
import {createStore,combineReducers} from 'redux'
import reduxShape from 'redux-shape'
let leaf = {
state:"",
reducers:{
changeText(state,action){
let text = action.text
return text;
},
clearText(state,action){
return '';
}
}
}
let shape = {
text:()=>leaf,// a leaf should be returned inside a function.
}
let reducer = reduxShape(combineReducers,{shape:shape,delimiter:'.'})
let store = createStore(reducer)
This code snippet create a redux store using the reducer generated by redux-shape. Note that you should pass combineReducers of redux to redux-shape.
If you call store.getState(),you will get an object:
{
text:''
}
You can change your state by dispatching an action.
store.dispatch({type:'text.changeText',text:'I am a text'})
And now your state will be
{
text:'I am a text'
}
After you apply your shape, action types are determined. Action types follow this pattern:
<shapeProperty><delimiter><reducerName>
So for the shape
let shape = {
text:()=>leaf,// a leaf should be returned inside a function.
}
you can change text by store.dispatch({type:'text.changeText',text:'some text'}), and clear text by store.dispatch({type:'text.clearText'})(changeText and clearText reducers are defined in leaf).
Assume that you have defined a nested state (note:nested state is not recommended in a redux app) shape as:
let nestedShape = {
a:{
text:()=>leaf
},
b:{
text:()=>leaf
}
}
You can change state.a.text by store.dispatch({type:'a.text.changeText',text:'some text'}), and change state.b.text by store.dispatch({type:'b.text.changeText',text:'some text'}).
You can integrate redux-shape with react-router-redux easily if you want to use react-router in you app.
react-router-redux need to add custom reducers to store, we can use custom config like below:
import {createStore,combineReducers} from 'redux'
import {routerReducer} from 'react-router-redux'
import reduxShape from 'redux-shape'
let leaf = {
state:"",
reducers:{
changeText(state,action){
let text = action.text
return text;
},
clearText(state,action){
return '';
}
}
}
let shape = {
text:()=>leaf,// a leaf should be returned inside a function.
}
let reducer = reduxShape(combineReducers,{
shape:shape,
delimiter:'.',
custom:{
router:routerReducer
}
})
let store = createStore(reducer)
FAQs
A simple util for generating redux action & reducer by a state shape definition.
The npm package redux-shape receives a total of 4 weekly downloads. As such, redux-shape popularity was classified as not popular.
We found that redux-shape demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.