我正在开发一个React Native应用程序,其中我使用Redux从API中提取数据。现在,我只想让组件在API数据发生变化时重新呈现。如果API数据与之前相同,则不应更改。我如何使用Redux来实现这一点?
发布于 2021-03-11 07:21:09
我已经使用useSelector钩子成功地完成了这项工作:
https://react-redux.js.org/api/hooks#useselector
使用API中的数据更新您的redux存储。每当redux存储中的数据发生变化时,钩子都会触发,从而允许在redux存储发生变化时重新呈现任何组件。
import { useSelector } from 'react-redux';
const MyComponent = () => {
const apiData = useSelector(state => state.apiDataLocation);
/*This will update to display the api data whenever the
store is updated.*/
return (
<View>
<Text>{apiData}</Text>
</View>
);
};
https://stackoverflow.com/questions/66571559
复制相似问题