2

Very new to react, I have a class like:

export const Checkbox = ({ label, className }) => {
  ....
  .... 
  return(
    .............
    .............

  );             
}

In such a class how can I specify add a componentDidMount and componentWillReceiveProps?

Edit:

I have a layman's doubt, which is the correct way to create a wrapper react component for checkbox? functional component or by extending from Component? If I create a Checkbox wrapper component for others to use, will it be possible for them to connect to a store (say redux) from their pages?

Thanks, Santhosh

1
  • 1
    You cannot. That's actually invalid syntax. You would want to replace render with return. To utilize lifcycle methods use must create a class which extends React.Component Commented Aug 18, 2017 at 22:39

3 Answers 3

3

You can't.

These are functional components and do not support React's lifecycle methods.

If you want to use React's lifecycle methods, you need a class that inherits from React.Component

See example below.

class Example extends React.Component {
    
   componentDidMount(){
     //code here
   }
    
}

Here is a good answer explaining the differences and when to use functional over class components.

UPDATE from React 16.8

Lifecycle methods can now be used with the introduction of hooks.

If you wished to implement componentDidMount you would use the useEffect hook and pass an empty array as the second argument. An example would look like as follows.

const Example = () => {
  React.useEffect(() => {
    // do stuff here
  }, []);
}
Sign up to request clarification or add additional context in comments.

2 Comments

typo in source code - should be extends React.Component
@john_omalley well spotted! I should really go to sleep here!
2

You can't.

If you want to use React's lifecycle methods, you need a class that inherits from React.Component.

export class Checkbox extends React.Component {
    componentDidMount() {
        ....
    }

    render() {
        const { label, className } = this.props;
        ....
    }
}

3 Comments

@ I have a layman's doubt, which is the correct way to create a wrapper react component for checkbox? functional component or by extending from Component? If I create a Checkbox wrapper component for others to use, will it be possible for them to connect to a store (say redux) from their pages?
@kalladaYou can use either to create your own wrapper of a checkbox. Redux works with supplying props to a component, so any component that renders based on props will work.
@paul_fitzgerald I'm a bit confused why your answer, which has literally copied the main sentence from my answer is now changed to the accepted answer.
1

You cannot. You should create a class that extends PureComponent or Component if you need lifecycle methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.