【问题标题】:My mapStateToProps is not called after adding custom object array to redux state将自定义对象数组添加到 redux 状态后,未调用我的 mapStateToProps
【发布时间】:2021-02-06 21:58:15
【问题描述】:

我尝试了几个小时,但无法弄清楚为什么在添加 自定义对象array 后没有调用我的状态。

// In my component...
const myRemoteArray = getRemoteArray() // Is working
props.addAdItems(myRemoteArray) // Calls **1 via component.props
/// ...


const mapDispatchToProps = (dispatch) => {
    return {
        addAdItems: (items) => { // **1
            // Items contains my array of objects 
            dispatch(addAdItems(items)) // Calls **2
        },
    }
}

// My action 
export const addAdItems = (items) => { // **2
    // Items contains my array of objects 
    return { // Calls **3
        type: AD_ITEMS,
        adItems: items,
    }
}

const productsReducer = (state = initialState, action) => {
    switch (action.type) { // **3
        case AD_ITEMS:
            // Is working! 
            // action.adItems contains my array!
            const _state = {
                ...state,
                adItems: action.adItems, // Here is the issue, I am not sure how to add my NEW array to existing state and update it. 
                                         // Like that: ??? "adItems: ...action.adItems" or adItems: [action.adItems]
            }

            // The new state contains my Array!!!
            return _state

        default:
            return state
    }
}

// In my component... !!!!
// THIS IS NOT CALLED or it is called with empty array from initialState!!!
const mapStateToProps = (state) => { 
    return {
        updatedItem: state.changedItem,
        adItems: state.adItems,
    }
}



在我看来,Redux 对包含以下数据的数组存在问题。我的类方法有 Redux 问题吗?

class Ad {
    constructor(
        id,
        isPublished
    ) {
        this.id = id
        this.isPublished = isPublished
    }

    someMessage = () => { return "Help me!" }
    needHelp = () => { return true }
}

我的 Redux 正在使用其他调用、数据和对象,这意味着我的 createStore 和所有其他内容都是正确的。

PS:我没有多家商店。

更新 现在我的 mapDispatchToProps 被当前数组调用,但没有持久化。

更新 2 如果我保存文件并强制刷新应用程序,props.adItems 包含 我加载的数组,但如果我想在运行时访问 props.adItems(例如在 FlatList刷新)又是空数组!

为什么? 在通过 useEffect 进行更改后,我应该将数组存储在 useState 属性中吗?

【问题讨论】:

    标签: javascript reactjs react-native redux react-redux


    【解决方案1】:

    您在减速器中添加的 cmets 非常接近,但它们都不是 100% 准确的。

    为了让 Redux 注意到您的数组已更改,您需要新状态的属性 adItems 来返回一个全新的数组。你可以这样做:

    adItems: [...action.adItems]
    

    使用此代码,您将创建一个新数组,然后将旧数组的项的副本添加到其中。

    您当前的实现 (adItems: action.adItems) 不起作用的原因是 action.adItems 实际上是对内存中数组的引用。即使数组内容发生了变化,action.adItems 的值仍然是相同的,一个指向当前存储数组位置的指针。这就是你的 store 没有被更新的原因:因为 Redux 不检查数组本身的值,而是检查数组存储位置的引用,所以你返回的新状态正是一样,所以 Redux 不知道有任何变化。

    【讨论】:

    • 嗯有道理,我会测试一下
    • 现在我的 mapDispatchToProps 被新数组调用但没有持久化。
    【解决方案2】:

    正如 LonelyPrincess 所说,我在其他地方提出了这个问题,如果你这样做 xArray = yArra 这意味着按引用而不是按值调用。

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 2020-08-08
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      相关资源
      最近更新 更多