Professional Documents
Culture Documents
Reactjs Class Component: By:-Love Kothari
Reactjs Class Component: By:-Love Kothari
Reactjs Class Component: By:-Love Kothari
1. Class component
2. States
3. Prps
5. Events/Methods
7. Context
8. Error Boundaries
Class Components
Class Components
State’s
state is dynamic, it enables a
component to keep track of
changing information in between
renders and for it to be dynamic
and interactive. State can only be
used within a class component.
Example of State
class Test extends React.Component {
render() {
constructor () { return (
<div>
this.state = {
<p>{this.state.id}</p>
id: 1,
<p>{this.state.name}</p>
name: "test" </div>
);
}; }
} }
Prop’s :
If you're already familiar with how arguments & functions work in JavaScript, understanding props is a
piece of cake! In a nutshell, props are used to pass data from a parent component to a child
component in React and they are the main mechanism for component communication.
}
Class component life cycle
In the past, JavaScript errors inside components used to corrupt React’s internal state and cause it to
emit cryptic errors on next renders. These errors were always caused by an earlier error in the
application code, but React did not provide a way to handle them gracefully in components, and could
not recover from them.
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
1
this.state = { hasError: false }; }
render() {
static getDerivedStateFromError(error) {
if (this.state.hasError) {
// Update state so the next render will show the
// You can render any custom fallback UI
fallback UI.
return <h1>Something went wrong.</h1>;
return { hasError: true };
}
}
return this.props.children;
}
}
componentDidCatch(error, errorInfo) {
3
// You can also log the error to an error reporting service
logErrorToMyService(error, errorInfo);
}
2
Thank-you :-
Before starting a live demo anyone has any
queries please let me know