Interview question
What are the differences between Class and Functional components? Class और Functional components में क्या अंतर है?
Answer
| Aspect | Class | Functional |
|---|---|---|
| Syntax | ES6 class | JavaScript function |
| State | this.state | useState hook |
| Lifecycle | Lifecycle methods | useEffect hook |
| This binding | Required | Not needed |
| Performance | Heavier | Lighter with hooks |
// Class Component
class Welcome extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return <h1>Count: {this.state.count}</h1>;
}
}
// Functional Component
function Welcome() {
const [count, setCount] = useState(0);
return <h1>Count: {count}</h1>;
}| Aspect | Class | Functional |
|---|---|---|
| Modern approach | Legacy (पहले का तरीका) | Modern (अब preferred) |
Was this answer clear?