index.js:
import { legacy_createStore } from "redux";
import reducer from './reducer'
const store = legacy_createStore(reducer)
export default store
reducer.js:
const initialState = {
num: 20,
currentSession: {}
}
const reducer = (state = initialState, action) => {
const newState = JSON.parse(JSON.stringify(state))
switch(action.type) {
case 'add':
newState.num += action.val
case 'transSession':
newState.currentSession = action.val
}
return newState
}
export default reducer
组件:文章来源:https://www.toymoban.com/news/detail-615103.html
import { useSelector, useDispatch } from 'react-redux'
// 获取
const num = useSelector(state => state.num)
// 更新
const dispatch = useDispatch()
const testChangeNum = () => {
dispatch({ type: 'add', val: 3 })
}
src/index.js:文章来源地址https://www.toymoban.com/news/detail-615103.html
import { Provider } from "react-redux";
import store from '@/app/redux/index';
<Provider store={store}>
<App />
</Provider>
到了这里,关于react-redux:的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!