简体   繁体   English

React Native,Redux 不更新 state 上的组件,在 combineReducer 之后更改

[英]React Native, Redux not updating components on state change after combineReducer

Stack Overflow Gods,堆栈溢出神,

I am new to Redux and am working on pulling data from a database and using it to create an initial state object for Redux using Redux Thunk.我是 Redux 的新手,正在从数据库中提取数据并使用它为 Redux 使用 Redux Thunk 创建初始 state object。

My reducer file structure currently looks like:我的减速器文件结构目前看起来像:

  • reducers减速器
    • index.js索引.js
    • initialData.js初始数据.js

My components re-renders perfectly when I have the reducer that handles these interactions within index.js:当我拥有在 index.js 中处理这些交互的 reducer 时,我的组件可以完美地重新呈现:

 import {GET_INITIAL_DATA_FULFILLED, GET_INITIAL_DATA_PENDING, GET_INITIAL_DATA_REJECTED} from '../constants/action-types' const initialState = { chats: [], messages: [], user: {}, loading:true, errorMessage:'' } const reducer = (state = initialState, action) => { switch(action.type){ case GET_INITIAL_DATA_PENDING: // console.log('loading') return {...state, loading:action.payload }; case GET_INITIAL_DATA_FULFILLED: // console.log('Data recieved') return {...state, user: action.payload.user, messages:action.payload.messages, chats: action.payload.chats, loading:action.loading }; case GET_INITIAL_DATA_REJECTED: // console.log('Error getting data') return {...state, errorMessage: action.payload, loading: action.loading }; default: return state; } } export default reducer

However, when I want to decouple this from the root reducer and switch to using combineReducer, the components no longer re-renders.但是,当我想将它与根减速器分离并切换到使用 combineReducer 时,组件不再重新渲染。 This is that implementation:这是那个实现:

index.js索引.js

 import { combineReducers } from 'redux'; import initialData from './initialData' export default combineReducers({ initialData })

initialData.js初始数据.js

 import {GET_INITIAL_DATA_FULFILLED, GET_INITIAL_DATA_PENDING, GET_INITIAL_DATA_REJECTED} from '../constants/action-types' const initialState = { chats: [], messages: [], user: {}, loading:true, errorMessage:'' } const initialData = (state = initialState, action) => { switch(action.type){ case GET_INITIAL_DATA_PENDING: console.log('loading') return {...state, loading:action.payload }; case GET_INITIAL_DATA_FULFILLED: console.log('Data recieved') return {...state, user: action.payload.user, messages:action.payload.messages, chats: action.payload.chats, loading:action.loading }; case GET_INITIAL_DATA_REJECTED: console.log('Error getting data') return {...state, errorMessage: action.payload, loading: action.loading }; default: return {...state}; } } export default initialData

If anybody has any ideas as to why my component is not re-rendering, despite the data being received and the state being updated I would love to hear them.如果有人对为什么我的组件没有重新渲染有任何想法,尽管收到了数据并且更新了 state,我很乐意听取他们的意见。

Figured it out.弄清楚了。 combineReducer creates a new property on the state object. So the component was just rendering an undefined state as I wasn't checking for that new property. combineReducer 在 state object 上创建了一个新属性。因此该组件只是呈现一个未定义的 state,因为我没有检查该新属性。

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

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