Junior
Kuidas õigesti muuta Reacti komponentide olekut?
sobes.tech AI
Vastus AI-lt
Change of state in React occurs via the setState method for class components or using the useState hook for functional components.
Class components:
// Import React
import React, { Component } from 'react';
// Create a class component
class MyComponent extends Component {
// Initialize state
constructor(props) {
super(props);
this.state = {
count: 0
};
}
// Method to change state
incrementCount = () => {
// Use `setState`
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<p>Counter: {this.state.count}</p>
<button onClick={this.incrementCount}>Increase</button>
</div>
);
}
}
// Export the component
export default MyComponent;
setStateis asynchronous.setStatemerges the new state with the current one (shallow merge).setStatecan accept a function that returns a new state based on the previous one:
// Method to change state using a function
incrementCountFunction = () => {
this.setState((prevState) => ({
count: prevState.count + 1
}));
};
Functional components (useState):
// Import React and useState
import React, { useState } from 'react';
// Create a functional component
function MyFunctionComponent() {
// Initialize state with useState
const [count, setCount] = useState(0); // [current state, function to update]
// Function to change state
const incrementCount = () => {
// Use update function
setCount(count + 1);
};
return (
<div>
<p>Counter: {count}</p>
<button onClick={incrementCount}>Increase</button>
</div>
);
}
// Export the component
export default MyFunctionComponent;
useStatereturns an array of two elements: current state and a function to update it.- The update function returned by
useStatereplaces the current state, it does not merge. - You can pass a function to the update function if the new state depends on the previous one:
// Function to change state using a function
const incrementCountFunction = () => {
setCount(prevCount => prevCount + 1);
};
Important points when changing state:
- Do not modify state directly: Never assign a new value to the
stateproperty directly (e.g.,this.state.count = 5orcount = 5). React will not detect this change and the component will not re-render. - Modifying objects and arrays: When changing objects or arrays in state, always create new copies. React compares references, and if the reference does not change, React assumes data has not changed and does not trigger re-rendering.
// Changing an object in a class component
this.setState(prevState => ({
user: {
...prevState.user, // Copy old properties
name: 'New Name' // Update the needed property
}
}));
// Changing an array in a functional component
setItems(prevItems => [...prevItems, 'New Element']); // Add new element
- Asynchronous
setState: If you need to perform an action after the state has been updated and the component has re-rendered, use the second argument ofsetState(callback function) oruseEffecthook in functional components.
// Callback in `setState`
this.setState({ count: this.state.count + 1 }, () => {
console.log('State updated!');
});
// Using `useEffect` to react to state change
useEffect(() => {
console.log('Count changed:', count);
}, [count]); // Dependency: will run when `count` changes
Comparison of methods:
| Feature | Class component (setState) |
Functional component (useState) |
|---|---|---|
| Syntax | this.setState(newState[, callback]) |
const [state, setState] = useState(initialState) hook |
| Merging/Replacing | Merges new state with current (shallow merge) | Replaces current state with new |
| Asynchronous | Asynchronous | The update function is asynchronous when called from event handlers, but synchronous during React's render phase |
| Dependency on previous state | Pass a function to setState: this.setState(prevState => ({...})) |
Pass a function to the update function: setState(prev => ({...})) |