Javascript React/Redux不呈现nextState

Javascript React/Redux不呈现nextState,javascript,reactjs,redux,react-redux,rendering,Javascript,Reactjs,Redux,React Redux,Rendering,在研究这个问题时,我的问题的常见原因是改变状态,而不返回状态的新对象,这会导致redux无法识别更改。然而,这不是,也从来不是一个问题,我很清楚这一点。我正在返回一个新对象。在您可以在所附图像中看到的记录器中,它显示已成功解析的api调用,并且nextState已更新但从未呈现。刷新页面的行为完全相同,即使我希望在初始登录到根页面时可能需要这样做 组成部分: import pokemonReducer from '../../reducers/pokemon_reducer'; import P

在研究这个问题时,我的问题的常见原因是改变状态,而不返回状态的新对象,这会导致redux无法识别更改。然而,这不是,也从来不是一个问题,我很清楚这一点。我正在返回一个新对象。在您可以在所附图像中看到的记录器中,它显示已成功解析的api调用,并且nextState已更新但从未呈现。刷新页面的行为完全相同,即使我希望在初始登录到根页面时可能需要这样做

组成部分:

import pokemonReducer from '../../reducers/pokemon_reducer';
import PokemonIndexItem from './pokemon_index_item';
import {Route} from 'react-router-dom';
import PokemonDetailContainer from './pokemon_detail_container';


class PokemonIndex extends React.Component {
    
    componentDidMount() {
        this.props.requestAllPokemon();
    }

    render() {
        const pokemon = this.props.pokemon;
        
       

        return (
            <section className="pokedex">
                <Route path='/pokemon/:pokemonID' component={PokemonDetailContainer} />
                <ul>{pokemon && pokemon.map(poke => <li>{poke.name}{poke.id}</li>)}</ul>
            </section>
        );
    }
}

export default PokemonIndex;
减速器:

import { RECEIVE_ALL_POKEMON, RECEIVE_SINGLE_POKEMON} from '../actions/pokemon_actions';



const pokemonReducer = (initialState = {}, action) => {
    Object.freeze(initialState);
    switch(action.type) {
        
        case RECEIVE_ALL_POKEMON:
            return Object.assign({}, initialState, action.pokemon);
        case RECEIVE_SINGLE_POKEMON:
            let poke = action.payload.pokemon
            return Object.assign({}, initialState, {[poke.id]: poke})
        default: 
            return initialState;
    }
};

export default pokemonReducer;
二级减速器:

import { combineReducers } from 'redux';
import pokemonReducer from './pokemon_reducer'


const entitiesReducer = combineReducers({
    pokemon: pokemonReducer,
});

export default entitiesReducer;
减根剂:

import {combineReducers} from 'redux';
import entitiesReducer from './entities_reducer';

const rootReducer = combineReducers({
    entities: entitiesReducer
});

export default rootReducer;
根据要求,这里是在reducers文件夹中定义的选择器

export const selectAllPokemon = (state) => {
    Object.values(state.entities.pokemon);
};

export const selectSinglePokemon = (state) => {
    Object.values(state.entities.pokemon)
};

以下是创建的操作:


export const RECEIVE_ALL_POKEMON = "RECEIVE_ALL_POKEMON";
export const RECEIVE_SINGLE_POKEMON = "RECEIVE_SINGLE_POKEMON";
import * as APIUtil from '../util/api_util';

export const receiveAllPokemon = (pokemon) => (
    {
        type: RECEIVE_ALL_POKEMON,
        pokemon
    }
);

export const requestAllPokemon = () => (dispatch) => {
    APIUtil.fetchAllPokemon()
        .then(
            pokemon => 
            { dispatch(receiveAllPokemon(pokemon));}
        );
        
};

export const receiveSinglePokemon = data => (
    {
        type: RECEIVE_SINGLE_POKEMON,
        data
    }
);

export const requestSinglePokemon = id => (dispatch) => {
    APIUtil.fetchSinglePokemon(id)
        .then(pokemon => {dispatch(receiveSinglePokemon(pokemon));
        return pokemon;});
};


正如您在问题中所述,您的redux状态得到了正确设置,但您的新状态从未被呈现,我认为这与您的选择器有关。在我看来,你忘了返回你的计算状态

export const selectAllPokemon = (state) => {
    Object.values(state.entities.pokemon);
};
// will return undefined
要返回您的状态,您有两个选项:

显式返回

 export const selectAllPokemon = (state) => {
   return Object.values(state.entities.pokemon);
};
 export const selectAllPokemon = (state) => (
   Object.values(state.entities.pokemon);
 );
隐含回报

 export const selectAllPokemon = (state) => {
   return Object.values(state.entities.pokemon);
};
 export const selectAllPokemon = (state) => (
   Object.values(state.entities.pokemon);
 );

我参考这一点,或者看看我在中创建的示例,以便更好地取消arrow函数中隐式和显式返回的稳定性。

const-mapStateToProps=state=>({pokemon:selectAllPokemon(state)})
,你能给我看看这个selectAllPokemon选择器吗?同时提供你在pokemonReducer上面定义动作和初始状态的代码。谢谢你看。这让我质疑我对react redux的理解程度。我已经用请求的代码片段更新了帖子。