0.1.0 • Published 11 months ago
redext v0.1.0
redext
A simple global store based on React Context and Hooks
Installation
npm install redext
or
yarn add redext
Use It
// store/models/common.js
import React from 'react';
export default {
state: {
theme: 'white',
showModal: false
},
effects: (dispatch) => {
return {
onOpenModal(modalState) {
this.updateState({
showModal: true,
modalState
});
// or
dispatch({
type: 'common/updateState',
payload: {
showModal: true,
modalState
}
});
// or
dispatch.common.updateState({
showModal: true,
modalState
})
}
}
},
reducers: {
updateState: (state, payload) => {
return {
...state,
...payload
};
},
changeTheme: (state) => {
return {
...state,
theme: state.theme === 'white' ? 'dark' : 'white'
};
}
}
}
// store/models/index.ts
import common from './common';
import auth from './auth';
export default {
common,
auth
}
// store/index.ts
import { Provider, connect, init, models } from 'redext';
import models from './models';
export { Provider, connect, init, models }
// client/index.ts
import { Provider, init, models } from './store';
const store = init({
models
});
const App = ({ children, initialValue = {} }) => {
return (
<Provider store={store} initialValue={initialValue}>
{children}
</Provider>
)
}
export default App
// client/pages/movie.js
import { connect, memoize, useDeepEffect } from 'redext';
const Movie = ({ dispatch, theme, onOpenModal }) => {
useDeepEffect(() => {
}, [array, object]);
return (
<div>
{theme}
</div>
)
}
const mapState = ({ common: { theme } }) => ({
theme
});
const mapDispatch = ({ common: { onOpenModal } }) => ({
onOpenModal
});
// options
const memoPropsAreEqual = (prevProps, nextProps) => {
if (nextProps.visible !== prevProps.visible) {
return false
}
return true
};
export default connect(mapState, mapDispatch)(memoize(Movie, memoPropsAreEqual))
0.0.15
11 months ago
0.0.16
11 months ago
0.0.17
11 months ago
0.0.1-6.1
11 months ago
0.0.16-1
11 months ago
0.0.16-3
11 months ago
0.0.16-2
11 months ago
0.0.16-5
11 months ago
0.0.16-4
11 months ago
0.0.16-7
11 months ago
0.0.16-6
11 months ago
0.1.0
11 months ago
0.0.17-dev-1
11 months ago
0.0.17-dev-2
11 months ago
0.0.13-staging
1 year ago
0.0.13
1 year ago
0.0.14
1 year ago
0.0.13-staging-1
1 year ago
0.0.13-staging-2
1 year ago
0.0.10
2 years ago
0.0.11
2 years ago
0.0.12
2 years ago
0.0.7-1
3 years ago
0.0.9
3 years ago
0.0.8-4
3 years ago
0.0.8-3
3 years ago
0.0.8
3 years ago
0.0.8-2
3 years ago
0.0.8-1
3 years ago
0.0.7-2
3 years ago
0.0.7
4 years ago
0.0.6
4 years ago
0.0.4-3.staging
4 years ago
0.0.4-8.staging
4 years ago
0.0.4-2.staging
4 years ago
0.0.4-4.staging
4 years ago
0.0.4-staging
4 years ago
0.0.4-5.staging
4 years ago
0.0.4-6.staging
4 years ago
0.0.4-7.staging
4 years ago
0.0.4-1.staging
4 years ago
0.0.5
4 years ago
0.0.4
4 years ago
0.0.3
4 years ago
0.0.2
4 years ago
0.0.1
4 years ago