saslam23
saslam23

Reputation: 3

React Component to Functional Component

I want to convert this to a function component

This is using the React library and I believe I have most of it right. I think it has something to do with the fetchDraftsTimer function but I am not entirely sure what I am missing.

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;
    }

}

I have done this

import React, { useEffect } from "react";

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

    props.fetchHistory;

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

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


  return null;
}

Am I missing something? Apparently both of these are missing some logic. Please help!

Upvotes: 0

Views: 85

Answers (1)

Jay Pillai
Jay Pillai

Reputation: 178

props.fetchHistory;
props.fetchDrafts;

You have to call these functions, exacly like in the class component. You're only supposed to use the above in case you want to pass this function itself down to a child component.

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

Upvotes: 1

Related Questions