Notes
Notes
1. Types of Components:
In React, there are two main types of components: functional components and class
components.
Functional Components: These are JavaScript functions that return JSX. They are
also known as stateless components because they don't have internal state (prior to
React 16.8 and the introduction of hooks).
function FunctionalComponent() {
return <div>This is a functional component</div>;
}
Class Components: These are JavaScript classes that extend the React.Component
class. Class components can manage their own internal state.
Props allow you to pass data from a parent component to a child component.
Props are read-only and help make components reusable.
Props are passed as attributes to a component in JSX.
function Greeting(props) {
return <div>Hello, {props.name}</div>;
}
// Usage
<Greeting name="John" />
3. State:
State is used to manage data that can change over time within a component.
State is specific to class components and functional components using hooks.
You can initialize state in the constructor (for class components) or using the
useState hook (for functional components).
render() {
return (
<div>
<p>Count: {this.state.count}</p>
</div>
);
}
}
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
4. Lifecycle Methods (Class Components):
The render method (in class components) or the return statement (in functional
components) specifies what should be rendered to the DOM.
Components can render other components, creating a hierarchy of components.
6. Reusability:
Controlled components are components that rely entirely on props and state for
their data and behavior.
Uncontrolled components, on the other hand, manage some aspects of their state
internally and are less predictable in behavior.
9. Presentational vs. Container Components:
Presentational components are concerned with how things look and are often
stateless.
Container components are concerned with how things work and manage data and state.
10. Component Lifecycle (Class Components):
- Class components have a series of lifecycle methods that can be used to manage
component behavior, such as componentDidMount (called after the component is
inserted into the DOM), componentDidUpdate (called after a component's props or
state changes), and componentWillUnmount (called before a component is removed from
the DOM).
function CounterFunctional() {
const [count, setCount] = useState(0);
return (
<div>
<h2>Functional Counter</h2>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
decrement = () => {
this.setState({ count: this.state.count - 1 });
};
render() {
return (
<div>
<h2>Class Counter</h2>
<p>Count: {this.state.count}</p>
<button onClick={this.increment}>Increment</button>
<button onClick={this.decrement}>Decrement</button>
</div>
);
}
}