【问题标题】:ListView dont render new item after state updateListView 在状态更新后不呈现新项目
【发布时间】:2016-06-01 11:25:55
【问题描述】:

我有一个使用 Redux 的 React-Native 项目。

我在组件“ParentComponent”中嵌入了组件“Comments”。

添加评论后,我的“评论”组件会收到更新后的状态,并使用道具中的新数组调用渲染函数。但不要在 listView 上绘制新单元格。

我的 ActionComments 代码

添加评论:

if(parent_id){
  commentsList[parent_index].responses.push(responseObject)
}else{
  commentsList.splice(0, 0, responseObject)
}
Actions.pop()

var newList = commentsList
dispatch(commentsListUpdate(newList))

评论列表更新:

function commentsListUpdate(list) {
  return {
    type: types.COMMENTS_LIST_UPDATE,
    list
  };
}

我的减速机:

case types.COMMENTS_LIST_UPDATE:
  return {
    ...state,
    isFetching: false,
    list: action.list
  };

我的组件:

class Comments extends Component {
  render(){
    let ds = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 !== r2
    });
    let dataSource = ds.cloneWithRows(this.props.list);

    return (
        <ListView
         dataSource={dataSource}
         renderRow={this._renderRow.bind(this)}
        />
    )
  }

  _renderRow(rowData: string, sectionID: number, rowID: number) {
        return (
            <View style={Styles.viewStyle}>
                <Comment
                    name={rowData.user.full_name}
                    avatar={rowData.user.photo_dir}
                    comment={rowData.content}
                    list={rowData.responses}
                    dispatch={this.props.dispatch}
                    owner_id={this.props.owner_id}
                />
            </View>
        )
  }
}

【问题讨论】:

  • 你能分享一下你的代码吗?尤其是更新数据源的部分。

标签: listview reactjs react-native render redux


【解决方案1】:

你能告诉我们你的减速器吗?我认为您已经通过像这样推送新元素来使状态静音:

case 'ADD_ITEM':
    return state.push(item)

如果你改变状态,redux 将看不到 OLD 状态和 NEW 状态之间的区别。相反,您必须返回一个新数组:

case 'ADD_ITEM':
    return [...state, item]

编辑 (compiled code)

case 'ADD_ITEM':
    return [].concat(state, [item])

【讨论】:

  • 如何将它应用到我的代码中?我的州有清单。 const initialState = { list:[], isFetching: false, };
  • 我尝试:case types.COMMENTS_LIST_UPDATE: return { ...state, isFetching: false, list: [].concat(action.list) }; 我认为这不是问题,因为我的组件调用渲染函数并且列表处于新项目的状态,但不绘制新元素列表。
  • 是的,它在减速器部件上看起来不错。你能告诉我更多关于你的渲染部分吗?
  • 好的,我把它加到主帖上
  • 您是否尝试先删除数据源检查差异?
猜你喜欢
  • 2020-08-07
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多