项目中集成redux
WeChate0a3742bab79a66e34e01fe63dbba765.png
文件夹action profile文件和reducer文件下profile文件名保持一致
action->profile

function  receiveProfile (profile) {
    return {
        type:  'PROFILE.FETCH',
        profile
    }
}
export {
    receiveProfile
}

reducer->profile

export  default (state = {}, action) => {
    switch (action.type) {
    case  'PROFILE.FETCH':
    return {
        data:  action.profile
    }
    default:
        return  state
    }
}

两个文件中的type对应case要一致

reducer->index

import {combineReducers} from  "redux";
import  profile  from  './profile'
const  rootReducer  =  combineReducers({
    profile
});
export  default  rootReducer;

需要将文件导出出来

触发action的文件中引入

import {connect} from  'react-redux'
import { bindActionCreators } from  'redux';
this.props.dispatch(receiveProfile('叮当叮当')) //需要触发action的方法中写入这一行
export  default  connect( ( { bindActionCreators } ) => ( { bindActionCreators } ) )( LoginRegisterContainer ); // export default LoginRegisterContainer;换成connect写法

使用redux数据的文件中引入

import {connect} from  'react-redux'
module.exports  =  connect(function (state, ownProps) {
return {
    profile:  state.profile
}
})(Login) // export default Login;(原写法)

在使用时直接可以通过this.props.profile获取


_布凯茜
17 声望2 粉丝