1)What is the difference between a functional component and a class component in React?
Functional Components:
- Functional component JavaScript function that returns JSX UI elements.
- Easy to read, and with React Hooks, it can manage state and side effects
Example:
`function Greeting (props){
return(
<h1> Hello ,{props.name}</h1>;
)
}`
Class Component:
- Class component is a JavaScript class that extends React.Component
- Has access to lifecycle methods (componentDidMount, componentDidUpdate, etc.)
- Before Hooks, class components were the only way to use state and lifecycle logic.
- Class components take more code and are less simple than functional components.
Example:
`class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}`
Top comments (0)