[英]Multiple states in one React component?
我无法弄清楚如何在单个 React 组件中使用多个状态,因为我无法在 class 中调用 useState。
我需要实现的是拥有多个状态并使用 useEffect 独立地“监视”它们。
目前我有这样的东西,但据我了解,我不能有任何像 useEffect 仅基于 object 字段的东西。 我应该如何 go 关于这个?
class ShowPosts extends Component {
constructor(props) {
super(props);
this.state = {
posts: [],
sorting:'desc',
lastPostDate:'none',
hasMore:false,
pageNumber:1
};
}
class ShowPosts extends Component {
constructor(props) {
super(props);
this.state = {
posts: [],
sorting:'desc',
lastPostDate:'none',
hasMore:false,
pageNumber:1
};
componentDidUpdate(prevProps, prevState, snapshot){
//write the code to monitor this.state and prevState in here
}
}
ComponentDidUpdate is called after every render. This will be same as useEffect for functional components.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.