编辑:通过引入,Hooks可以实现生命周期类型的行为以及功能组件中的状态。目前
Hooks 是一个新的特性提案,它可以让你在不编写类的情况下使用状态和其他 React 特性。它们作为v16.8.0的一部分在 React 中发布
useEffecthook 可用于复制生命周期行为,useState并可用于在函数组件中存储状态。
基本语法: 
useEffect(callbackFunction, [dependentProps]) => cleanupFunction
您可以在钩子中实现您的用例,例如
const grid = (props) => {
    console.log(props);
    let {skuRules} = props;
    useEffect(() => {
        if(!props.fetched) {
            props.fetchRules();
        }
        console.log('mount it!');
    }, []); // passing an empty array as second argument triggers the callback in useEffect only after the initial render thus replicating `componentDidMount` lifecycle behaviour
    return(
        <Content title="Promotions" breadcrumbs={breadcrumbs} fetched={skuRules.fetched}>
            <Box title="Sku Promotion">
                <ActionButtons buttons={actionButtons} />
                <SkuRuleGrid 
                    data={skuRules.payload}
                    fetch={props.fetchSkuRules}
                />
            </Box>      
        </Content>  
    )
}
useEffect还可以返回一个将在卸载组件时运行的函数。这可用于取消订阅侦听器,复制以下行为componentWillUnmount:
例如:componentWillUnmount
useEffect(() => {
    window.addEventListener('unhandledRejection', handler);
    return () => {
       window.removeEventListener('unhandledRejection', handler);
    }
}, [])
要以useEffect特定事件为条件,您可以为其提供一组值以检查更改:
例如:componentDidUpdate
componentDidUpdate(prevProps, prevState) {
     const { counter } = this.props;
     if (this.props.counter !== prevState.counter) {
      // some action here
     }
}
钩子等效
useEffect(() => {
     // action here
}, [props.counter]); // checks for changes in the values in this array
如果您包含此数组,请确保包含随时间变化的组件作用域中的所有值(props、状态),否则您最终可能会引用先前渲染中的值。
使用有一些微妙之处useEffect;查看 API Here。
v16.7.0 之前
函数组件的属性是它们无法访问 Reacts 生命周期函数或this关键字。React.Component如果要使用生命周期函数,则需要扩展该类。
class Grid extends React.Component  {
    constructor(props) {
       super(props)
    }
    componentDidMount () {
        if(!this.props.fetched) {
            this.props.fetchRules();
        }
        console.log('mount it!');
    }
    render() {
    return(
        <Content title="Promotions" breadcrumbs={breadcrumbs} fetched={skuRules.fetched}>
            <Box title="Sku Promotion">
                <ActionButtons buttons={actionButtons} />
                <SkuRuleGrid 
                    data={skuRules.payload}
                    fetch={props.fetchSkuRules}
                />
            </Box>      
        </Content>  
    )
  }
}
当您只想渲染您的组件而不需要额外的逻辑时,函数组件非常有用。