๐React - ืืืจืื
ืืืืื ืฉืื ืืืจ ืฉืื ืขื ื ืืืื ืฆื ืืคืจืงืื ื ืคืจืืื.

useState โ Basics
ืชืืื ืขื ืืื ืื
1) ืื ืื useState?
useState
ืืื Hook ืฉืืืคืฉืจ ืืจืืืื ืคืื ืงืฆืื ืืจืืืงื ืืืืืืง ืืฆื (state). ืืื ืืืืืจ ืืขืจื ืขื ืฉื ื ืืืืจืื:
โข ืืขืจื ืื ืืืื ืฉื ืึพstate
โข ืคืื ืงืฆืื ืฉืืขืืื ืช ืืช ืึพstate (ืืขืืืื ืืืจืจ re-render ืฉื ืืจืืื)
ืึพHook ืืงืื ืขืจื ืืชืืืชื ืืืจืืืื ื (ืืืจืืจืช ืืืื ืฉื ืึพstate). ืื ืงืจืืื ืืคืื ืงืฆืืืช ืืขืืืื ืชืื ืืก ืขืืืื ืืชืืจ, ืึพReact ืชืจื ืืจ ืืืืฉ ืืช ืืจืืื ืขื ืืขืจื ืืืขืืืื.
ืืืืืื ืืืื ื ืจืื ืืช ืืชืืืช ืืืกืืก ืฉื useState โ ืืื ืืืืืจืื state, ืืืื ืืืฆืขืื destructuring ืืื ืืงืื ืืช ืืขืจื ืืืช ืคืื ืงืฆืืืช ืืขืืืื.
import { useState } from 'react';
// ืืชืืื ืืกืืกืืช:
const [stateValue, setStateValue] = useState(defaultValue);
// ืืืืื ืงืฆืจื:
const [isOpen, setIsOpen] = useState(false);
setIsOpen(true); // ืืืจืื ื-re-render ืขื isOpen=true
2) Destructuring (Array)
ืืืืืื ืฉึพuseState
ืืืืืจ ืืขืจื, ืืงืืื ืืืฉืชืืฉ ืึพArray Destructuring ืืื ืืฉืืืฃ ืืช ืฉื ื ืืืืืจืื ืึพ2 ืืฉืชื ืื. ืืฉื ืืจืืฉืื ืืื ืึพstate, ืืืฉื ื ืืื ืคืื ืงืฆืืืช ืืขืืืื (ืืงืืื ืืืืกืืฃ ืงืืืืืช set
).
ืื ืขืืฉืื Destructuring ืืฆืืจื ืงืจืืื: ืฉื ืึพstate ืืกืืืจ ืื ื ืฉืืจ, ืืฉื ืคืื ืงืฆืืืช ืืขืืืื ืืกืืืจ ืื ืืื ืืขืืื ืช.
const [count, setCount] = useState(0);
const [name, setName] = useState('peter');
3) ืืืืื ืืกืืกืืช: Counter
ืืื ื ืฉืชืืฉ ืึพuseState
ืืื ืืืืืืง ืืื ื. ืื ืืืืฆื ืขื ืืืคืชืืจ ืชืงืคืืฅ ืืช ืืขืจื. ืฉืื ืื: ืงืจืืื ืึพsetCount
ืชืืจืื ืึพre-render.
ืืืืืื: count
ืืื ืืขืจื, ืึพsetCount
ืืื ืคืื ืงืฆืืืช ืืขืืืื. ืืจืืจืช ืืืื 0, ืืืืืืฆื ืื ืื ื ืฉืืืืื ืขืจื ืืืฉ.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<>
<p>count: {count}</p>
<button className='btn' onClick={() => setCount(count + 1)}>
+
</button>
</>
);
}
ืืืืื ืืื
4) ืืื ืกืืืืืื ื ืคืจืืื (name/age/hobby)
ืืคืขืืื ื ืื ืืืคืจืื ืืื ืขืจืืื ืฉืื ืื ืึพstate ื ืคืจื ืืื ืฉืื. ืื ืืืคื ืขืืืื ืื ืืคืฉืืืื ืืืืืงืืื.
ืืื ื ืืืืจ ืฉืืืฉื ืกืืืืืื: name
, age
, hobby
. ืืคืชืืจ ืืื ืืขืืื ืืช ืฉืืืฉืชื.
import { useState } from 'react';
const UseStateObject = () => {
const [name, setName] = useState('peter');
const [age, setAge] = useState(24);
const [hobby, setHobby] = useState('read books');
const displayPerson = () => {
setName('john');
setAge(28);
setHobby('scream at the computer');
};
return (
<>
<h3>{name}</h3>
<h3>{age}</h3>
<h4>Enjoys To: {hobby}</h4>
<button className='btn' onClick={displayPerson}>
show john
</button>
</>
);
};
export default UseStateObject;
ืืืืื ืืื
5) ืกืืืื ืืืืืืืงื ืืื
ืืืงืื ืืื ืกืืืืืื, ืืคืฉืจ ืืจืื ืืืชื ืืืืืืืงื ืืื. ืื ืฉืืืืฉื ืืฉืฉืืืช ืงืฉืืจืื ืืื. ืืฉืื: ืื ืืขืืื ืื ืจืง ืฉืื ืืื, ืืฉ ืืืฉืชืืฉ ืึพspread
ืืื ืื ืืืจืืก ืืืจืื.
ืืื ืื ื ืืืืืงืื ืืช ืื ืคืจืื ืืืฉืชืืฉ ืชืืช person
, ืืืขืืื ืื ืืช ืื ืืืืืืืงื ืืืืืฆื.
import { useState } from 'react';
const UseStateObject = () => {
const [person, setPerson] = useState({
name: 'peter',
age: 24,
hobby: 'read books',
});
const displayPerson = () => {
setPerson({ name: 'john', age: 28, hobby: 'scream at the computer' });
// ืขืืืื ืืืงื ื ืืื:
// setPerson(prev => ({ ...prev, name: 'susan' }));
};
return (
<>
<h3>{person.name}</h3>
<h3>{person.age}</h3>
<h4>Enjoys To: {person.hobby}</h4>
<button className='btn' onClick={displayPerson}>
show john
</button>
</>
);
};
export default UseStateObject;
ืืืืื ืืื
6) ืขืืืื ืคืื ืงืฆืืื ืื โ ืืฉืืขืจื ืชืืื ืืขืจื ืืงืืื
ืืื ืฆืจืื? ืืงืจืืื ืึพsetState
ืื ืืขืืื ืช ืืืืืืช โ ืืืื ืื ืืขืจื ืืืืฉ ืชืืื ืืขืจื ืืงืืื, ืขืืืฃ ืืืฉืชืืฉ ืืฆืืจื ืืคืื ืงืฆืืื ืืืช: setState(prev => next)
. ืื ืืืืื ืฉืชืฉืชืืฉื ืืขืจื ืืขืืื ื.
ืืืืืื ืืจืืฉืื ื ืืขืื ืืื ื ืืืืื. ืืืืืื ืืฉื ืืื ืืจืื ืืื ืืขืฉืืช ืืืช ืื ืืืจื ืืฉืืืื (timeout).
const [value, setValue] = useState(0);
// ืชืืื ืืขืจื ืืงืืื:
setValue(prev => prev + 1);
// ืืืืืืงื ืชืืืึพืขืจื:
setState(prev => ({ ...prev, count: prev.count + 1 }));
ืืืืื ืืื
7) ืืชื ืืชืืฆืข Re-render?
- ืื ืงืจืืื ืึพ
setState
ืฉืืืืืคื ืขืจื ืืืฉืื ืืืฉ ืชืืจืื ืึพre-render. - React 18 ืขืืฉื Auto-batching: ืืื ืขืืืื ืื ืฆืืืืื ืืืืชื event-loop ืืืืืื ืืจืื ืืืจ ืืื.
- ืื ืชืืคืืกื ืืืื ืืื ืืืจื
setState
, ืชืจืื ืืช ืืขืจื ืืืฉื. ืืจืื ืืืจ ืืืืฉ ืืืืข ืืื ืืืืจ ืืื.
๐ ืืงืืจืืช ืืืืืฆืื (Docs) โ useState
ืขืืืื ืชืืขืื ืจืฉืืืื ืืืื ืช useState ืืขืืืง, ืืคืืกื ืขืืืื ืืืจืืืืงืืืจืช state:
- React Docs โ useState (Reference)
- React Docs โ State as a Snapshot (ืืื ืืืฉืื ืขื State)
- React Docs โ Queueing a Series of State Updates (ืชืืจืื ื-batching)
- React Docs โ Updating Objects in State (ืขืืืื ืืืืืืงืืื ื ืืื ืขื spread)
- React Docs โ Updating Arrays in State (ืื ืืขืช ืืืืฆืื ืืืขืจืืื)
- React Docs โ Choosing the State Structure (ืืื ืืืืืจ ืืื ื state)
- React Docs โ Preserving and Resetting State (ืืชื ืืฉืืจ/ืืืคืก state)
- React Docs โ Sharing State Between Components (ืืจืืช state)