强制组件在 redux 状态更改后重新渲染

yes*_*day 2 reactjs react-native redux

我有一个带有 2 个屏幕的堆栈导航器,“消息”和“消息”。在“消息”屏幕上,我想了解与我聊天的所有人以及对话中的最后一条消息。我将所有对话存储在 redux 中,而不是在服务器上。我的 redux 状态如下所示:

const state = {
    user = [
        {
            userId: 1,
            username: John,
            messages: [...array of messages send to John or from John...]
        },
        {
            userId: 2,
            username: Jane,
            messages: [...array of messages send to Jane or from Jane...]
        }
        ...
    ]
}
Run Code Online (Sandbox Code Playgroud)

在“消息”屏幕上,我有一个 FlatList 组件,它在 redux 状态下遍历用户数组:

const users = useSelector(state => state.messages.users)

const renderUserItem = user => {
    return (
        <View>
            <Text>{user.username}</Text>
            <Text>{user.messages[user.messages.length-1].message</Text>
        </View>
    )
}

<FlatList
    data={messages}
    renderItem={({item, index}) => renderUserItem(item)} />
Run Code Online (Sandbox Code Playgroud)

这行得通,我可以大致了解我的对话 + 最后一条消息。当我单击“消息”屏幕上的对话时,我将发送到“消息”屏幕,在那里我有另一个包含此特定对话的所有消息的 FlatList。此代码无关紧要,但可以使用。

问题是:当我添加一条新消息(通过“消息”屏幕)时,redux 状态会更新:(dispatch(addMessage(userId, message)),但是当我导航回“消息”屏幕时,我看不到最后一条消息,即使 redux 状态改变,屏幕也不会重新渲染。当我进行硬刷新时,它会起作用。

当 redux 状态发生变化时,如何强制屏幕重新渲染。我正在使用无状态组件。

编辑:我的减速机:

const initialState = {
    users: []
}

const MessagesReducer = (state = initialState, action) => {
    let array, index, user, messages
    switch (action.type) {
        case 'START_CONVERSATION':
            // action.user is an object which contains userId and username
            array = state.users
            index = array.findIndex(e => e.userId === action.user.userId)
            if (index > -1) {
                // conversation already exists
            } else {
            object = {
                userId: action.user.userId,
                username: action.user.username,
                messages: []
            }
            array.push(object)
            return {
                users: array
            }
        break
        case 'ADD_MESSAGE':
             // action.userId contains the userId of the conversation partner
             // action.message is an object with direction, 
             // message and timestamp in it
             array = state.users
             index = array.findIndex(e => e.userId === action.userId)
             user = array[index]
             user.messages.push({
                 direction: action.message.direction,
                 message: action.message.message,
                 timestamp: action.message.timestamp
             }
             // get the old user object out of the array
             array.splice(index, 1)
             // push the new user object to the beginning of the array
             array.unshift(user)  
             return {
                user: array
             }
        break
        default:
            return state
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

Bri*_*son 5

你正在改变状态,这导致它看起来好像根本没有状态变化。这意味着没有重新渲染。

将此更改array = state.usersarray = [...state.users].

这将创建一个新的数组引用而不是改变先前的状态,这将导致按预期重新渲染。


再看一眼,我发现了更多的问题。这些不会停止重新渲染,但仍会改变先前的状态,因此应该修复它。

改变这个:

user = array[index] // user is still a reference to previous state
user.messages.push({ // messages is still a reference to previous state
  ...
Run Code Online (Sandbox Code Playgroud)

对此:

// Create a new user object based on the previous values
// Then add to the messages array by creating a new array with your new entry
user = {
  ...array[index], 
  messages: [
    ...array[index].messages, 
    {
      direction: action.message.direction,
      message: action.message.message,
      timestamp: action.message.timestamp
    }
  ]
}

// OR
user = {...array[index], messages: [...array[index].messages]}
user.messages.push({...})
Run Code Online (Sandbox Code Playgroud)

  • 我强烈鼓励您使用[我们的官方 Redux Toolkit 包](https://redux-toolkit.js.org)。它默认捕获突变并抛出错误来警告您。它还允许您使用“变异”语法编写不可变的更新逻辑,这将大大简化减速器。 (2认同)