【发布时间】: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