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
組件:文章來(lái)源:http://www.zghlxwxcb.cn/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:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-615103.html
import { Provider } from "react-redux";
import store from '@/app/redux/index';
<Provider store={store}>
<App />
</Provider>
到了這里,關(guān)于react-redux:的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!