React Action,Reducer&连接语法

时间:2017-09-25 07:02:22

标签: reactjs redux react-redux store connect

我有以下React Code在Redux商店中创建一个booleen true / false,然后用于打开/关闭Material UI Drawer / Side Menu。

我是React的新手,我想问一下我的工作是否正确,或者我是否犯了明显的错误等。

注意:解决方案有效(它按预期打开/关闭抽屉)。我只是想知道我是否应该以不同的方式进行编码...我也删除了一些代码,以便更容易阅读......

动作档案:

export const setDrawerPopOutMenuStatus = {
    type: 'DRAWER_POPOUT_MENU_STATUS'
}

Reducers文件:

import { combineReducers } from 'redux';

const setDrawerPopOutMenuStatus = (state = true, action) => {
    switch (action.type) {
        case 'DRAWER_POPOUT_MENU_STATUS':
            if(state) {
                return false;
            }else{
                return true;
            }
        default:
            return state;
    }
}

export default combineReducers({
    setDrawerPopOutMenuStatus
})

存储文件

import { createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import reducer from './reducers.js';
import { setDrawerPopOutMenuStatus } from './actions.js';


const store = createStore(reducer, composeWithDevTools());

const render = () => {
    console.dir(store.getState());
};

store.subscribe(render);
render();

export default store;

Index.js(开始档案):

    import React from 'react';
    import ReactDOM from 'react-dom';
    import { Provider } from 'react-redux';
    import store from './store.js';
    import './index.css';
    import App from './components/App.js';
    import registerServiceWorker from './registerServiceWorker';

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

最后一个组件(这会将状态传递给子组件):

import React from 'react'
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { setDrawerPopOutMenuStatus } from "../actions";


class App extends React.Component {

    // Redux Drawer State (Toggle PopOut Menu)
    drawerPopOutHandle = () => {
        this.props.drawerPopOutUpdated();
    }


    // PreLoad Actions (eg: make action run once to start with)
    componentDidMount() {
        this.props.drawerPopOutUpdated()
    }


    render() {
        return (
                <LeftDrawerMenu drawerStatus={this.props.drawerStatus}/>
        )
    }
}





App.propTypes = {
    drawerStatus: PropTypes.bool
};

const mapStateToProps = (state) => {
    console.log('drawer status: '+state.setDrawerPopOutMenuStatus);

    return {
        drawerStatus: state.setDrawerPopOutMenuStatus
    }
}

const mapDispatchToProps = (Dispatch) => {
    return({
        drawerPopOutUpdated: () => Dispatch(setDrawerPopOutMenuStatus)
    })
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

1 个答案:

答案 0 :(得分:1)

为什么不像下面的const那样采取行动?使用对象而不是单个值存储状态也非常方便。

action.js

/*Action*/
export const DRAWER_POPOUT_MENU_STATUS = 'DRAWER_POPOUT_MENU_STATUS';

/*Action Creator*/
export const setDrawerPopOutMenuStatus = {
    type: DRAWER_POPOUT_MENU_STATUS,
}

reducers.js

import { combineReducers } from 'redux';
import { DRAWER_POPOUT_MENU_STATUS } from './action';

const initialState = {
    someName: true,
};

const setDrawerPopOutMenuStatus = (state = initialState, action) => {
    switch (action.type) {
        case DRAWER_POPOUT_MENU_STATUS:
            let newState = {};
            newState['someName'] = !state.someName;
            return Object.assign({}, state, newState);
        default:
            return state;
    }
}

这使得以后在项目规模更大时更容易管理。

相关问题