mapStateToProps 中的状态有错误的数据

state in mapStateToProps has wrong data

我很确定我有一些小的配置错误,因为我确实有这个工作。但是当我通过 react-redux connect 调用点击我的 mapStateToProps 时,它被注入了我的减速器。我的设置是:

App.js:

import React from 'react';
import BuildsContainer from '../containers/builds-container';
import NavBarContainer from '../containers/nav-bar-container';

class App extends React.Component {
    constructor(props) {
        super(props);  
    }

    render() {
        return (
            <div>
                <NavBarContainer />

                <div className="container body-content">

                    <BuildsContainer />

                    <hr />
                    <footer>
                        <p>&copy; 2016</p>
                    </footer>

                </div>
            </div>
        );
    }
}

export default App;

导航栏-container.js:

import { connect } from 'react-redux'
import NavBar from '../components/nav-bar'
import  { setUsername } from '../actions/actions'

const mapStateToProps = (state) => {
    console.log(state)  
    return {username: state.username}
}

const mapDispatchToProps = ({ 
    setUsername: setUsername
}) 

const NavBarContainer = connect(
mapStateToProps,
mapDispatchToProps
)(NavBar)

export default NavBarContainer

我的主减速器,reducer.js:

import { combineReducers } from 'redux'
import UsernameReducer from './username-reducer'
import BuildsReducer from './builds-reducer'

const BuildWatcherApp = combineReducers({
    UsernameReducer,
    BuildsReducer
})

export default BuildWatcherApp;

和我的用户名-reducer.js:

const UsernameReducer = (state = '', action) => {
    switch (action.type) {
        case 'SET_USERNAME':
            return action.username
        default:
            return state;
    }
}

export default UsernameReducer;

我在 mapStateToProps 通话中 console.log(state) 的位置显示:

似乎 state 里面有我的减速器。这似乎不对,我期待它有 username 道具。如果值得注意,mapDispatchToProps 似乎确实传递了正确的函数。

有人能告诉我我错过了什么吗?

原来我的问题是 combineReducers:

const BuildWatcherApp = combineReducers({
   UsernameReducer,
   BuildsReducer
})

我的 reducer 名称与我所在州的属性不匹配。将其更改为:

const BuildWatcherApp = (state = {}, action) => ({
    username: UsernameReducer(state.username, action),
    builds: BuildsReducer(state.builds, action)
})

已修复。