React-Redux-this.props返回始终未定义

时间:2019-01-17 13:36:28

标签: javascript node.js reactjs mongodb redux

我有以下代码

store / index.js

const DEFAULT_STATE = {
    auth: { isAuthenticated: false },
    error: { message: null },
    tracks: [],
    uploadedTrack: {}
};

store / reducers / index.js

import auth from './auth';
import error from './error';
import {tracks, uploadedTrack} from './tracks';

export default combineReducers({
    auth,
    tracks,
    uploadedTrack,
    error
});

store / reducers / tracks.js

import {UPLOADED_TRACK, SET_CURRENT_USER_TRACK} from '../actionTypes';

export const tracks = (state = [], action) => {
    switch(action.type) {
        case SET_CURRENT_USER_TRACK:
            return action.tracks;
        default:
            return state;
    }
}

export const uploadedTrack = (state = {}, action) => {
    switch(action.type) {
        case UPLOADED_TRACK:
            return action.track;
        default:
            return state;
    }
};

store / actions / tracks.js

export const setTrack = tracks => ({
    type: SET_CURRENT_USER_TRACK,
    tracks
});

export const setUploadedTrack = track => ({
    type: UPLOADED_TRACK,
    track
});

export const getUserTrack = () => {
    return async dispatch => {
        try {
            const {token, ...tracks} = await api.call('get', 'tracks/user');

            dispatch(setTrack(tracks));
            dispatch(removeError());
        } catch (err) {
            const {error} = err.response.data;
            dispatch(addError(error.message));
        }
    };
};

components / trackList.jsx

componentDidMount() {
        const {getUserTrack} = this.props;
        getUserTrack();
    }

render() {
        var {authType} = this.props;
        const {auth} = this.props;
        const {tracks} = this.props;

        console.log("Track: ", tracks)

所有操作似乎都可行,因为我在Redux商店上的“曲目”包含我的六个曲目列表,但是当我尝试从控制台上的“ tracks”变量打印此信息时,此打印为“ undefined”。 奇怪的是,我在控制台上的“通话”包含我的六个音轨... 你能帮助我吗? 我不知道我的错误在哪里,我尝试应用网上找到的解决方案,但没有任何效果。

1 个答案:

答案 0 :(得分:1)

您能说明如何将redux状态映射到组件吗?如果您的redux商店商店包含您的六个轨道,但是您无法在react组件中显示它们,则问题可能出在您将商店绑定到组件的方式(当您在components/trackList.jsx中调用connect()时)。