• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

redux导入createStore间有条线,解决方案和redux的使用。react

武飞扬头像
weixin_43677787
帮助1

问题:
学新通
解决方案:
学新通
关于redux使用:
1.建立index.js文件

import { legacy_createStore as createStore, combineReducers } from 'redux'
import xxxReducer from './xxxreducer'

const reducer = combineReducers({ // combineReducers引入多个处理器
   	xxxReducer: xxxReducer
})

export default createStore(reducer)
  1. 建立xxReducer.js文件
const initState = {
    key:  ''
}
export default function menuNameReducer (state = initState, action) {
    const { type, date } = action;
    switch (type) {
        case '方法名':
            return {
                key: state.key
            }
        default: 
            return state
    }
}
  1. App.jsx中导入
import { Provider } from 'react-redux'
import store from './store'

ReactDOM.createRoot(document.getElementById('root')).render(
    <Provider store={store}>
      <App  />
    </Provider>
)

  1. 其他页面中提交方法
import { connect, useDispatch } from 'react-redux'

cont dispatch = useDispatch();
const onClick = () => {
	dispatch({
		type: 与xxReducer相同的方法名称,
		key: value
	)
}

4.其他页面获取xxxReducer的方法

import store from '../store'

console.log(store.getState().key) // 没有combineReducers的时候
console.log(store.getState().xxxReducer.key)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgahabe
系列文章
更多 icon
同类精品
更多 icon
继续加载