Skip to content

4 class components

Ben Foley edited this page Aug 9, 2019 · 3 revisions

Class Components

In the previous workshop we used Functional components, basic building blocks of React. React has two main types of components: functional and class.

Lets look at the different forms:

Functional:

const Name = props => {
  return <p>{props.firstName}</p>
}

Class:

class Name extends React.Component {
  render() {
    return <p>{this.props.firstName}</p>
  }
}

The biggest difference is that Class components have "state" and "lifecycle hooks". How to choose? Try to use functional components for presentational aspects of your project, and class components where some logic is required, or you need to use state.

Breaking news, functional components can now use state and hooks too... stay tuned for workshop update.

Let's build the toy app using class components, and add an input that uses local state.


Further reading

More detail about different types of components.

Functional components

Clone this wiki locally