Javascript 一个组件中有多个状态?

Javascript 一个组件中有多个状态?,javascript,reactjs,react-hooks,Javascript,Reactjs,React Hooks,我不知道如何在一个React组件中使用多个状态,因为我不能在一个类中调用useState 我需要实现的是拥有多个状态,并用useffect独立地“监控”它们 目前我有类似的东西,但据我所知,我不能有任何像useEffect这样的东西,仅仅基于对象字段。我该怎么做 class ShowPosts extends Component { constructor(props) { super(props); this.state = { posts: [],

我不知道如何在一个React组件中使用多个状态,因为我不能在一个类中调用useState

我需要实现的是拥有多个状态,并用useffect独立地“监控”它们

目前我有类似的东西,但据我所知,我不能有任何像useEffect这样的东西,仅仅基于对象字段。我该怎么做

class ShowPosts extends Component {
  constructor(props) {
    super(props);
    this.state = {
      posts: [],
      sorting:'desc',
      lastPostDate:'none',
      hasMore:false,
      pageNumber:1
    };

  }

你不能在课堂上使用
useffect
either@decpk所以我想要做的唯一方法就是将类重写为函数?钩子是设计用来在函数组件中使用的。因此,您需要重构成
functional
组件,或者使用
componentDidMount
生命周期来实现
副作用
    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.