Web / React native Interview questions
Difference between Functional component and class-based component in React.
| Functional component. | Class-based component. |
| Functional component cannot have state ( before React hooks, now it can have state with React 16.8 or higher). It renders component only based on props that are supplied from the parent. | A class-based component can have component level state. |
| Functional component are simple functions and light weighted. These are functions that accepts props as an argument and returns a React element. | A class-based component is a complex structure. It is an instance of a class derived from React. Component class. The class must implement a render() member function which returns a React component to be rendered. |
| functional component cannot use lifecycle methods. | The class-based component use lifecycle methods such as componentDidMount. |
Functional component example:
function Hello(props) { return <h1>Hello, {props.userName}</h1>; }
Class based component example:
class Hello extends React.Component { render() { return <h1>Hello, {this.props.userName}</h1>; } }
More Related questions...