In the following code extract, the skeleton of a component is presented. This format will be used to implement all components. To focus on the content, further examples will show only the constructor() and render() implementations. Full code examples can be downloaded from the Packt resources associated with this book:
// import React and other dependencies
import React, { Component } from 'react';
import AnotherComponent from './AnotherComponent';
// define the Component as a class
class MyComponent extends Component {
// optional method
constructor() {
super();
this.state = {
// state
};
}
// this method must be implemented
render() {
return (
// HTML definition
);
}
}
// export the component, so it can be used by others
export default MyComponent;