欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Redux

程序员文章站 2022-07-16 15:14:13
...

module/index.js

import {combineReducers} from 'redux'

const initData={
    name:'hkj'
}

export const setData=(data)=>{
    return {
        type:'SET_DATA',
        data
    }
}

const dataReducer=(state=initData,action)=>{
    switch(action.type){
        case 'SET_DATA':
            return {...state,action.data}
        default :
            return state
    }
}

const allReducer=combineReduers({
    dataReducer
})

allReduer.reducer='allReduer'
export default allReduer

HelloWorld.jsx

import {connect} from 'redux-react'
import {bindActionCreators} from 'redux'
import React,{Component} from 'react'
import {setData} from '../modules'

class HelloWrold extends Component {
    componentDidMount(){
        let data=this.state;
        this.props.setData(data)
    }
    render(){
        return (<div>{this.props.data.name}</div>)
    }
}

const mapDispatchToProps=(dispatch)=>{
    return {
        ...bindActionCreators({setData},dispatch)
    }
}
const mapStateToProps=(state)=>{
    return {
        data:state.allReducer.dataReducer.data
    }
}
export default connect(mapStateToProps,mapDispatchToProps)(HelloWorld)