Assignment 2
Assignment 2
ASSIGNMENT - 2
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './index'; // Update the import statement
import Component from './Component'; // Import the renamed component
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
component.jsx
import React from 'react';
ClassComponent.jsx
import React, { Component } from 'react';
class ClassComponent extends Component { render() {
return (
<div>
<h1>Hello, I am a Class Component!</h1>
</div>
);
}
}
export default ClassComponent
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import ClassComponent from './ClassComponent';
ReactDOM.render( <React.StrictMode>
<ClassComponent /> </React.StrictMode>, document.getElementById('root')
);
OUTPUT
3. What is the life cycle of React js? Explain by example and diagram.
In React.js, the lifecycle of a component refers to the series of methods that are invoked
at different stages of a component's existence. Understanding the component lifecycle
is crucial for managing state, performing side effects, and optimizing performance.
React 16.3 introduced a new lifecycle method approach which includes mounting,
updating, and unmounting phases.
Here's an overview of the lifecycle methods along with an example and a simplified
diagram:
Mounting Phase
constructor():
● Called when the component is initialized.
● Used for setting initial state and binding methods.
render():
● Responsible for rendering the component's UI.
● Must be a pure function, meaning it should not modify state or interact
with the DOM directly.
componentDidMount():
● Invoked after the component is rendered for the first time.
● Ideal for performing side effects like data fetching, initializing third-party
libraries, etc.
EXAMPLE
import React from 'react';
constructor(props) {
super(props);
componentDidMount() {
fetch('https://api.example.com/data')
render() {
return <div>{this.state.data}</div>;
}
Updating Phase
static getDerivedStateFromProps():
● Invoked right before calling the render method.
● Used to update the state based on changes in props.
shouldComponentUpdate():
● Determines if the component should re-render.
● Used for performance optimization by preventing unnecessary re-renders.
render(): (Already explained in Mounting Phase)
getSnapshotBeforeUpdate():
● Invoked right before changes from the virtual DOM are to be reflected in
the DOM.
● Used for capturing some information from the DOM (e.g., scroll position)
before it potentially changes.
componentDidUpdate():
● Invoked after the component's updates are flushed to the DOM.
● Ideal for performing side effects after a component has updated.
EXAMPLE
render() {
return <div>{this.props.data}</div>;
}
}
Unmounting Phase
componentWillUnmount():
● Invoked immediately before a component is unmounted and destroyed.
● Used for cleanup, such as removing event listeners or canceling network
requests.
EXAMPLE