I've had the pleasure of working with several other developers the last few weeks getting up to speed on React. A common cause of confusion is that so many tutorials have different ways of using classes. Some use a constructor and some do not. So, it's hard to know what is the "right" way.
I've recorded a very rough and unedited video with a few samples of the "old" and "new" ways to write classes in React to try to help clear this up. This also includes an overview of how to avoid using binding in the constructor.
Later, I'll try to do a video demonstrating how to take a React class component and convert it to a functional component with hooks.
Here's the link to the videos. The samples are listed below.
Sorry for the small fonts! I forgot to bump them up. All the samples below have the final code.
- React Class with Constructor
- React Class without Constructor
- React Class with Constructor and Method Binding
- React Class with No Constructor nor Method Binding
- React Class with Constructor That Puts Props in State
- React Class without Constructor That Puts Props in State
If you have any questions or suggestions for improvement on this, please reach out to me on Twitter.