我有一个称为“数据”的道具,它最初是在应用程序加载时向状态填充数据记录的。我要做的是根据我在mapsDispatchToprops中启动的函数将其附加到“data”属性。
目前代码如下:
class ListScreen extends React.Component {
static navigationOptions = {
header: null,
};
static propTypes = {
navigation: PropTypes.object.isRequired,
data: PropType.isRequired,
nextBatchOfData: PropTypes.func,
};
getData = () => {
this.props.nextBatchOfData({ some, search, params });
};
render() {
const { data } = this.props;
return (
<View style={styles.container}>
<ScrollView>
<StatusBar barStyle="default" />
<FlatList
data={data}
keyExtractor={({ id }) => `${id}`}
numColumns={2}
/>
</ScrollView>
</View>
);
}
}
const mapStateToProps = state => {
return {
data: [...dataSelectors.getOfferResults(state)], // Attempting to append to existing state..
};
};
const mapDispatchToProps = dispatch => ({
nextBatchOfData: searchParams => dispatch(actions.dataSearch.request(searchParams)),
});
当运行
getData()
nextBatchOfData()
道具会开火。但是,上一个状态将被覆盖。有什么办法绕过这个吗?
[combineActions(actions.dataSearch.success, actions.dataSearch.fail)]: (
state,
{ payload, meta, error }
) => {
const hasResults = payload && !error;
return {
...state,
isLoading: false,
error: error && payload.message,
results: hasResults ? payload : state.results,
};