0

I'm using react 17.0.2 with Typescript. I'm trying to pass setLoading from the parent component (App) to the child component (About) such that the loading state in App can be set from About.

interface AboutProps {
  setLoading: (arg: boolean | undefined) => void
}

const About: React.FC<AboutProps> = ({ setLoading }) => {
  const fields = [
       ...
  ];

  const { data, loading, error } = useFetch('About', fields);

  useEffect(() => setLoading(loading), [loading]);

  return (
    <div className="aboutComponent">
        <div>
          <Title title={data.custom_title} />
          <Card
            text={data.body}
          >
            <Skills
              skills_={data.skills}
              skillYears_={data.skill_years}
              columns_={data.n_skills_columns}
            />
          </Card>
        </div>
    </div>
  );
};

However, ESLint is complaining about missing props validation for setLoading in line 1. The setLoading function is passed from the parent component here:

const App = () => {
  const [loading, setLoading] = useState<any>(false);

  return (
    <div>
      <About setLoading={setLoading} />
    </div>
  );
};

I haven't seen this error before and I'm not sure what to do, since I already type the props with AboutProps interface.

DSteman
  • 1,388
  • 2
  • 12
  • 25

1 Answers1

1

I'm new to TypeScript as well, but I believe you can do this:

const About = ({ setLoading }:AboutProps) => {

Also, React TypeScript Cheatsheet has a section about React.FC being discouraged.

Dave
  • 7,552
  • 4
  • 22
  • 26