react-redux拆分合并

//store.js文件

import { createStore, compose, combineReducers } from 'redux'

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose

// 创建 store createStore(reduce)
// combineReducers 用于合并reducer
const store = createStore(
    combineReducers({
        home: home,
        login: login,
    }), 
    composeEnhancers()
)
//home的reducer
const homeState = {
    name: 'home',
}
function home(state = homeState, action) {
    switch (action.type) {
        case 'HOME_NAME':
            // return { ...state, name: action.payload }
            return { ...state, name: '111' }

        default:
            return state
    }
}
//login的reducedr
const loginState = {
    name: 'login',
}
function login ( state = loginState, action ){
    console.log(action)
    switch (action.type){
        case 'LOGIN_NAME':
            return { ...state, name: action.payload }
        default:
            return state
    }
}
//抛出store
export { 
    store
}

//index.js文件

import React from 'react';
import ReactDOM from 'react-dom';

import { Provider } from 'react-redux'
import { store } from '路径/store'

ReactDOM.render(
  
    

store配置

, document.getElementById('root') );

你可能感兴趣的:(react-redux拆分合并)