反应组件到功能组件

时间:2021-06-10 19:29:45

标签: javascript reactjs

我想把它转换成函数组件

这是使用 React 库,我相信我大部分都正确。我认为它与 fetchDraftsTimer 函数有关,但我不完全确定我错过了什么。

import React from 'react';

class MyComponent extends React.Component(props) {

    componentDidMount() {

        this.props.fetchDrafts()
        this.props.fetchHistory()

        this.fetchDraftsTimer = setInterval(() => {
            this.props.fetchDrafts();
        }, 120000);
    }

    render() {
        return null;
    }

}

我已经这样做了

import React, { useEffect } from "react";

export default function MyComponent(props) {
  useEffect(() => {
    props.fetchDrafts;

    props.fetchHistory;

    setInterval(() => {
      props.fetchDrafts;
    }, 120000);

    return () => {
    };
  }, []);


  return null;
}

我错过了什么吗?显然,这两者都缺少一些逻辑。请帮忙!

1 个答案:

答案 0 :(得分:1)

props.fetchHistory;
props.fetchDrafts;

你必须调用这些函数,就像在类组件中一样。仅当您想将此函数本身传递给子组件时,才应该使用上述内容。

props.fetchHistory();
props.fetchDrafts();