ReactJS lifecycle method inside a function Component

前端 未结 8 591
一生所求
一生所求 2020-11-30 19:28

Instead of writing my components inside a class, I\'d like to use the function syntax instead.

How do I override componentDidMount, componentWillM

8条回答
  •  佛祖请我去吃肉
    2020-11-30 20:08

    You can use react-pure-lifecycle to add lifecycle functions to functional components.

    Example:

    import React, { Component } from 'react';
    import lifecycle from 'react-pure-lifecycle';
    
    const methods = {
      componentDidMount(props) {
        console.log('I mounted! Here are my props: ', props);
      }
    };
    
    const Channels = props => (
    

    Hello

    ) export default lifecycle(methods)(Channels);

提交回复
热议问题