How to specify a constructor with a functional component (fat arrow syntax)?

后端 未结 5 780
南笙
南笙 2020-12-15 16:46

Given this component:

import React from \'react\'
import ReactDOM from \'react-dom\'
import PropTypes from \'prop-types\'

const NewGoalInput = props => {         


        
5条回答
  •  忘掉有多难
    2020-12-15 17:35

    To simulate constructor in FC use useEffect.

    useEffect(() => {
      ... here your init code
    }, []);
    

    That's it! EZ! This useEffect runs only once when the component loads and never runs after, just don't forget to add square brackets at the end.

提交回复
热议问题