简体   繁体   English

状态在React Redux中未正确更改

[英]State Not Changing Properly In React Redux

I am new to React Redux I have initialized state which is properly displaying by calling its respective reducer which job to return the initialize State. 我是React Redux的新手,我已经初始化了状态,可以通过调用其各自的reducer哪个作业来返回初始化状态来正确显示该状态。 But after mutating the state the state most probably not changing and i cant figure it out why 但是在改变状态后,状态很可能不会改变,我无法弄清楚为什么

import { combineReducers } from 'redux';

const initialState = {
    songs:[
        {title:'No Scrubs', duration: '4:05'},
    ]
}

const songReducers = (state = initialState)=>{
    return state
}

const selectedSongReducer =(selectedSong=null,action)=>{
    if(action.type==='SONG_SELECTED'){
        return action.payload
    }

    return selectedSong;
}


const addSongReducer = (state=initialState,action)=>{
    if(action.type==="ADD_SONG"){
       return {
        ...state,   
        songs:[
           ...state.songs, {title:'All demo', duration: '4:05'}
        ]
    }
    }
    return state;
}

export default combineReducers({
    songs: songReducers,
    selectedSong: selectedSongReducer,
    addSong:addSongReducer
})

Your overall state shape will look like 您的整体状态看起来像

state = {
    songs: {
        songs: [
            { title: 'No Scrubs', duration: '4:05' },
        ]
    },
    selectedSong: null,
    addSong: {
        songs: [
            { title: 'No Scrubs', duration: '4:05' },
        ]
    }
}

after the very first reducer call. 在第一个reduce调用之后。 Is this what you want? 这是你想要的吗? Asking because you have 问,因为你有

state = {
    songs: {
        songs: [
            { title: 'No Scrubs', duration: '4:05' },
        ]
    },
    ...
}

instead of just 而不只是

state = {
    songs: [
        { title: 'No Scrubs', duration: '4:05' },
    ]
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM