useState
useState
ã¯ãã³ã³ããŒãã³ãã« state å€æ° ãè¿œå ããããã® React ããã¯ã§ãã
const [state, setState] = useState(initialState);
- ãªãã¡ã¬ã³ã¹
- 䜿çšæ³
- ãã©ãã«ã·ã¥ãŒãã£ã³ã°
- state ãæŽæ°ããã®ã«å€ãå€ããã°ã«è¡šç€ºããã
- state ãæŽæ°ããã®ã«ç»é¢ãæŽæ°ãããªã
- âToo many re-rendersâ ãšãããšã©ãŒãåºã
- åæåé¢æ°ãæŽæ°çšé¢æ°ã 2 床åŒã°ãã
- é¢æ°ã state ã«ã»ããããããšãããšããã®é¢æ°ãåŒã³åºãããŠããŸã
ãªãã¡ã¬ã³ã¹
useState(initialState)
ã³ã³ããŒãã³ãã®ãããã¬ãã«ã§ useState
ãåŒã³åºããŠãstate å€æ°ã宣èšããŸãã
import { useState } from 'react';
function MyComponent() {
const [age, setAge] = useState(28);
const [name, setName] = useState('Taylor');
const [todos, setTodos] = useState(() => createTodos());
// ...
state å€æ°ã¯æ
£ç¿ãšããŠãåå²ä»£å
¥ãå©çšã㊠[something, setSomething]
ã®ããã«åœåããŸãã
åŒæ°
initialState
: state ã®åæå€ã§ããã©ããªåã®å€ã§ãæž¡ãããšãã§ããŸãããé¢æ°ãæž¡ããå Žåã¯ç¹å¥ãªæ¯ãèããããŸãããã®åŒæ°ã¯ååã¬ã³ããŒåŸã¯ç¡èŠãããŸããinitialState
ã«é¢æ°ãæž¡ããå Žåããã®é¢æ°ã¯åæåé¢æ° (initializer function) ãšããŠæ±ãããŸããåæåé¢æ°ã¯ãçŽç²ã§ãåŒæ°ãåãããäœããã®åã®å€ãè¿ãå¿ èŠããããŸããReact ã¯ã³ã³ããŒãã³ããåæåãããšãã«åæåé¢æ°ãåŒã³åºãããã®è¿ãå€ãåæ state ãšããŠä¿åããŸããäŸãèŠã
è¿ãå€
useState
ã¯ä»¥äžã® 2 ã€ã®å€ãæã€é
åãè¿ããŸãã
- çŸåšã® stateãååã¬ã³ããŒäžã§ã¯ã
initialState
ãšåãå€ã«ãªããŸãã - state ãå¥ã®å€ã«æŽæ°ããåã¬ã³ããŒãããªã¬ãã
set
é¢æ°ã
泚æç¹
useState
ã¯ããã¯ã§ãããããã³ã³ããŒãã³ãã®ãããã¬ãã«ãŸãã¯ã«ã¹ã¿ã ããã¯å ã§ã®ã¿åŒã³åºãããšãã§ããŸããã«ãŒããæ¡ä»¶æã®äžã§åŒã³åºãããšã¯ã§ããŸããããããå¿ èŠãªå Žåã¯ãæ°ããã³ã³ããŒãã³ããæœåºããstate ã移åãããŠãã ããã- Strict Mode ã§ã¯ãçŽç²ã§ãªãé¢æ°ãèŠã€ããããããããã«ãåæåé¢æ°ã 2 ååŒã³åºãããŸããããã¯éçºæã®ã¿ã®æ¯ãèãã§ãããæ¬çªã«ã¯åœ±é¿ããŸãããåæåé¢æ°ãçŽç²ã§ããã°ïŒããã§ããã¹ãã§ãïŒã2 ååŒã³åºãããŠãã³ãŒãã«åœ±é¿ã¯ãããŸããã2 åã®åŒã³åºãã®ãã¡ 1 åã®åŒã³åºãçµæã¯ç¡èŠãããŸãã
setSomething(nextState)
ã®ããã«å©çšãã set
é¢æ°
useState
ãè¿ã set
é¢æ°ãå©çšããŠãstate ãå¥ã®å€ã«æŽæ°ããåã¬ã³ããŒãããªã¬ããããšãã§ããŸããçŽæ¥æ¬¡ã® state ãæž¡ãããåã® state ãã次㮠state ãå°åºããé¢æ°ãæž¡ããŸãã
const [name, setName] = useState('Edward');
function handleClick() {
setName('Taylor');
setAge(a => a + 1);
// ...
åŒæ°
nextState
: 次㫠state ã«ã»ãããããå€ã§ããã©ããªåã®å€ã§ãæž¡ãããšãã§ããŸãããé¢æ°ãæž¡ããå Žåã¯ç¹å¥ãªæ¯ãèããããŸããnextState
ã«é¢æ°ãæž¡ããå Žåããã®é¢æ°ã¯æŽæ°çšé¢æ° (updater function) ãšããŠæ±ãããŸããæŽæ°çšé¢æ°ã¯ãçŽç²ã§ãåŠçäžã® state ã®å€ãå¯äžã®åŒæ°ãšããŠåãåãã次㮠state ãè¿ãå¿ èŠããããŸããReact ã¯æŽæ°çšé¢æ°ããã¥ãŒã«å ¥ããã³ã³ããŒãã³ããåã¬ã³ããŒããŸãã次ã®ã¬ã³ããŒã§ãReact ã¯ãã¥ãŒã«å ¥ãããããã¹ãŠã®æŽæ°çšé¢æ°ãåã® state ã«å¯ŸããŠé©çšãã次㮠state ãå°åºããŸããäŸãèŠã
è¿ãå€
set
é¢æ°ã¯è¿ãå€ãæã¡ãŸããã
泚æç¹
-
set
é¢æ°ã¯æ¬¡ã®ã¬ã³ããŒã®ããã® state å€æ°ã®ã¿ãæŽæ°ããŸããset
é¢æ°ãåŒã³åºããåŸã« state å€æ°ãèªã¿åã£ãŠããåŒã³åºãåã®ç»é¢ã«è¡šç€ºãããŠããå€ãå€ãè¿ãããŸãã -
æ°ããå€ãçŸåšã®
state
ãšåäžã®å ŽåãReact ã¯æé©åã®ããã«ãã³ã³ããŒãã³ããšãã®åã³ã³ããŒãã³ãã®åã¬ã³ããŒãã¹ãããããŸããstate ã®åäžæ§ã®æ¯èŒã¯ãObject.is
ã«ãã£ãŠè¡ãããŸããäžéšã®ã±ãŒã¹ã§ã¯ãReact ã¯åã³ã³ããŒãã³ããã¹ãããããåã«ã³ã³ããŒãã³ããåŒã³åºãå¿ èŠããããŸãããããªãã®ã³ãŒãã«åœ±é¿ãäžããããšã¯ãªãã¯ãã§ãã -
React 㯠state ã®æŽæ°ããŸãšããŠè¡ããŸãïŒãããåŠçïŒããã¹ãŠã®ã€ãã³ããã³ãã©ãå®è¡ãçµãã
set
é¢æ°ãåŒã³åºãããåŸã«ãç»é¢ãæŽæ°ããŸããããã«ããã1 ã€ã®ã€ãã³ãäžã«è€æ°åã®åã¬ã³ããŒãçºçããããšã¯ãããŸããããŸãã«ãæ©æã«ç»é¢ãæŽæ°ããå¿ èŠãããå ŽåïŒäŸãã° DOM ã«ã¢ã¯ã»ã¹ããå Žåãªã©ïŒããããŸããããã®å Žåã¯flushSync
ãå©çšã§ããŸãã -
ã¬ã³ããŒäžã«
set
é¢æ°ãåŒã³åºãããšã¯ãçŸåšã¬ã³ããŒäžã®ã³ã³ããŒãã³ãå ããã®ã¿èš±ãããŠããŸãããã®å ŽåãReact ã¯ãã®åºåãç Žæ£ããæ°ãã state ã§åã¬ã³ããŒãè©Šã¿ãŸãããã®ãã¿ãŒã³ãå¿ èŠã«ãªãããšã¯ã»ãšãã©ãããŸããããååã®ã¬ã³ããŒããã®æ å ±ãä¿åããããã«äœ¿çšã§ããŸããäŸãèŠã -
Strict Mode ã§ã¯ãçŽç²ã§ãªãé¢æ°ãèŠã€ããããããããã«æŽæ°çšé¢æ°ã 2 ååŒã³åºãããŸããããã¯éçºæã®ã¿ã®æ¯ãèãã§ãããæ¬çªã«ã¯åœ±é¿ããŸãããæŽæ°çšé¢æ°ãçŽç²ã§ããã°ïŒããã§ããã¹ãã§ãïŒã2 ååŒã³åºãããŠãã³ãŒãã«åœ±é¿ã¯ãããŸããã2 åã®åŒã³åºãã®ãã¡ 1 åã®åŒã³åºãçµæã¯ç¡èŠãããŸãã
䜿çšæ³
state ãã³ã³ããŒãã³ãã«è¿œå ãã
ã³ã³ããŒãã³ãã®ãããã¬ãã«ã§ useState
ãåŒã³åºãã1 ã€ä»¥äžã® state å€æ°ã宣èšããŸãã
import { useState } from 'react';
function MyComponent() {
const [age, setAge] = useState(42);
const [name, setName] = useState('Taylor');
// ...
state å€æ°ã¯æ
£ç¿ãšããŠãåå²ä»£å
¥ãå©çšã㊠[something, setSomething]
ã®ããã«åœåããŸãã
useState
ã¯ã以äžã® 2 ã€ã®å€ãæã€é
åãè¿ããŸãã
- ãã® state å€æ°ã®çŸåšã®å€ãæåã¯ãåæ state ã«èšå®ãããŸãã
- ã€ã³ã¿ã©ã¯ã·ã§ã³ã«å¿ããŠãstate ãä»ã®å€ã«å€æŽããããã®
set
é¢æ°ã
ã¹ã¯ãªãŒã³äžã®è¡šç€ºãæŽæ°ããã«ã¯ã次㮠state ãåŒæ°ãšã㊠set
é¢æ°ãåŒã³åºããŸãã
function handleClick() {
setName('Robin');
}
React ã¯æ¬¡ã® state ãä¿åããããšãæ°ããå€ã§ã³ã³ããŒãã³ããåã¬ã³ããŒããUI ãæŽæ°ããŸãã
äŸ 1/4: ã«ãŠã³ã¿ (number)
ãã®äŸã§ã¯ãcount
state å€æ°ãæ°å€ (number) ãä¿æããŠããŸãããã¿ã³ãã¯ãªãã¯ããããšã§ãæ°å€ãå¢å ããŸãã
import { useState } from 'react'; export default function Counter() { const [count, setCount] = useState(0); function handleClick() { setCount(count + 1); } return ( <button onClick={handleClick}> You pressed me {count} times </button> ); }
çŽåã® state ã«å¿ããŠæŽæ°ãã
age
ã 42
ã§ããå ŽåãèããŸãããããã®ãã³ãã©ã¯ãsetAge(age + 1)
ã 3 ååŒã³åºããŸãã
function handleClick() {
setAge(age + 1); // setAge(42 + 1)
setAge(age + 1); // setAge(42 + 1)
setAge(age + 1); // setAge(42 + 1)
}
ãããã1 åã¯ãªãã¯ããããšãage
㯠45
ã§ã¯ãªã 43
ã«ãªããŸãïŒ ããã¯ãset
é¢æ°ãåŒã³åºããŠããæ¢ã«å®è¡ãããŠããã³ãŒãã® age
state å€æ°ãæŽæ°ããããã§ã¯ãªãããã§ãããã®ãããsetAge(age + 1)
ã®åŒã³åºãã¯å
šãŠ setAge(43)
ã«ãªããŸãã
ãã®åé¡ã解æ¶ããããã次㮠state ã®ä»£ããã«ãæŽæ°çšé¢æ°ã setAge
ã«æž¡ãããšãã§ããŸãã
function handleClick() {
setAge(a => a + 1); // setAge(42 => 43)
setAge(a => a + 1); // setAge(43 => 44)
setAge(a => a + 1); // setAge(44 => 45)
}
ããã§ãa => a + 1
ã¯æŽæ°çšé¢æ°ã§ããæŽæ°çšé¢æ°ã¯ãåŠçäžã® state ã®å€ãåãåãããããã次㮠state ãå°åºããŸãã
React ã¯æŽæ°çšé¢æ°ããã¥ãŒã«å ¥ããŸãããããŠã次ã®ã¬ã³ããŒäžã«ãåãé çªã§æŽæ°çšé¢æ°ãåŒã³åºããŸãã
a => a + 1
ã¯åŠçäžã® state ã®å€ãšããŠ42
ãåãåãã次㮠state ãšããŠ43
ãè¿ããŸããa => a + 1
ã¯åŠçäžã® state ã®å€ãšããŠ43
ãåãåãã次㮠state ãšããŠ44
ãè¿ããŸããa => a + 1
ã¯åŠçäžã® state ã®å€ãšããŠ44
ãåãåãã次㮠state ãšããŠ45
ãè¿ããŸãã
ãã¥ãŒã«ã¯ãã以äžã®æŽæ°çšé¢æ°ã¯ãªãã®ã§ãReact ã¯æçµçã« 45
ãçŸåšã® state ãšããŠä¿åããŸãã
æ
£ç¿ãšããŠãåŠçäžã® state ã®åŒæ°åã«ã¯ãstate å€æ°åã®é æå 1 æåãå©çšããããšãäžè¬çã§ãïŒäŸãã°ãage
ãšãã state å€æ°ã«å¯ŸããŠãa
ãšããåŒæ°åïŒãããããprevAge
ãªã©ãä»ã®åãããããååã䜿ãããšãã§ããŸãã
éçºæã«æŽæ°çšé¢æ°ã 2 ååŒã³åºãããããšããããŸããããã¯ãæŽæ°çšé¢æ°ãçŽç²ã§ããããšã確èªããããã§ãã
ããã«æ·±ãç¥ã
æ°ããã»ããããå€ãçŽåã® state ããå°åºãããå Žåãåžžã« setAge(a => a + 1)
ãšããæžãæ¹ããã¹ãã ãšããæèŠããããŸããæªãããšã§ã¯ãããŸããããå¿
ãããå¿
èŠãªããã§ã¯ãããŸããã
ã»ãšãã©ã®ã±ãŒã¹ã§ã¯ãã©ã¡ãã®ã¢ãããŒãã§ãéãã¯ãããŸãããReact ã¯ãã¯ãªãã¯ãªã©ã®ãŠãŒã¶ã®æå³çãªã¢ã¯ã·ã§ã³ã«å¯ŸããŠãage
state å€æ°ã®æŽæ°ã次ã®ã¯ãªãã¯ã®åã«çºçããããšãä¿èšŒããŠããŸããããªãã¡ãã€ãã³ããã³ãã©ã®éå§æã«ãã¯ãªãã¯ãã³ãã©ããå€ããage
ãåç
§ããŠããŸãããšã¯ãããŸããã
äžæ¹ã§ãåãã€ãã³ãå ã§è€æ°åã®æŽæ°ãè¡ãå ŽåãæŽæ°çšé¢æ°ã圹ã«ç«ã¡ãŸãããŸããstate å€æ°èªèº«ãåç §ããããšãé£ããã±ãŒã¹ã«ãæçšã§ãïŒåã¬ã³ããŒã®çºçãæé©åããéã«ããã®ã±ãŒã¹ã«ééããããšããããŸãïŒã
ããããªææ³ã®åé·æ§ãããäžè²«æ§ãåªå ããã®ã§ããã°ãstate ãçŽåã® state ããå°åºãããå Žåã«ã¯ãåžžã«æŽæ°çšé¢æ°ãæžãããã«ããããšã¯åççã§ãããããstate ããä»ã® state å€æ°ã®çŽåã®å€ããå°åºãããå Žåã¯ããããã 1 ã€ã®ãªããžã§ã¯ãã«ãŸãšããŠãªãã¥ãŒãµ (reducer) ãå©çšããããšãæ€èšããŠãã ããã
äŸ 1/2: æŽæ°çšé¢æ°ãæž¡ã
ãã®äŸã§ã¯æŽæ°çšé¢æ°ãæž¡ããŠãããããâ+3â ãã¿ã³ã¯æ³å®éãã«åããŸãã
import { useState } from 'react'; export default function Counter() { const [age, setAge] = useState(42); function increment() { setAge(a => a + 1); } return ( <> <h1>Your age: {age}</h1> <button onClick={() => { increment(); increment(); increment(); }}>+3</button> <button onClick={() => { increment(); }}>+1</button> </> ); }
ãªããžã§ã¯ããé åã® state ãæŽæ°ãã
state ã«ãªããžã§ã¯ããé
åãã»ããããããšãã§ããŸãããã ã React ã§ã¯ãstate ã¯èªã¿åãå°çšãšããŠæ±ãå¿
èŠããããŸãããã®ãããstate ãæŽæ°ããå Žåã¯ãæ¢åã®ãªããžã§ã¯ããçŽæ¥æžãæãã (mutate) ã®ã§ã¯ãªãã眮ãæãã (replace) å¿
èŠããããŸããäŸãã°ãstate ãšã㊠form
ãªããžã§ã¯ããä¿æããŠããå Žåã以äžã®ããã«æžãæããè¡ã£ãŠã¯ãããŸããã
// ð© Don't mutate an object in state like this:
form.firstName = 'Taylor';
代ããã«ãæ°ãããªããžã§ã¯ããäœæããŠãªããžã§ã¯ãå šäœã眮ãæããŠãã ããã
// â
Replace state with a new object
setForm({
...form,
firstName: 'Taylor'
});
詳ããã¯ãstate å ã®ãªããžã§ã¯ãã®æŽæ°ããstate å ã®é åã®æŽæ°ãåç §ããŠãã ããã
äŸ 1/4: ãã©ãŒã ïŒãªããžã§ã¯ãïŒ
ãã®äŸã§ã¯ãform
state å€æ°ã¯ãªããžã§ã¯ããä¿æããŠããŸããããããã® input èŠçŽ 㯠change ãã³ãã©ãæã£ãŠãããæ°ãã form
ãªããžã§ã¯ããåŒæ°ãšã㊠setForm
ãåŒã³åºããŸãã{ ...form }
ã®ããã«ã¹ãã¬ããæ§æãçšããããšã§ãstate ãªããžã§ã¯ããïŒæžãæãã§ã¯ãªãïŒç¢ºå®ã«çœ®ãæããããšãã§ããŸãã
import { useState } from 'react'; export default function Form() { const [form, setForm] = useState({ firstName: 'Barbara', lastName: 'Hepworth', email: 'bhepworth@sculpture.com', }); return ( <> <label> First name: <input value={form.firstName} onChange={e => { setForm({ ...form, firstName: e.target.value }); }} /> </label> <label> Last name: <input value={form.lastName} onChange={e => { setForm({ ...form, lastName: e.target.value }); }} /> </label> <label> Email: <input value={form.email} onChange={e => { setForm({ ...form, email: e.target.value }); }} /> </label> <p> {form.firstName}{' '} {form.lastName}{' '} ({form.email}) </p> </> ); }
åæ state ãåçæãããããšãé²ã
React ã¯äžåºŠã ãåæ state ãä¿åãã2 åç®ä»¥éã®ã¬ã³ããŒã§ã¯ãããç¡èŠããŸãã
function TodoList() {
const [todos, setTodos] = useState(createInitialTodos());
// ...
createInitialTodos()
ã¯æ¯ã¬ã³ããŒã§åŒã³åºããããã®ã®ããã®çµæã¯ååã¬ã³ããŒã§ã®ã¿å©çšãããŸããããã¯ãcreateInitialTodos()
ã巚倧ãªé
åã®çæãã³ã¹ãã®é«ãèšç®ãè¡ã£ãŠããå Žåã«ãç¡é§ãå€ããªããŸãã
ããã解決ããããã以äžã®ããã«åæåé¢æ°ãæž¡ãããšãã§ããŸãã
function TodoList() {
const [todos, setTodos] = useState(createInitialTodos);
// ...
é¢æ°ãåŒã³åºããçµæã§ãã createInitialTodos()
ã§ã¯ãªããcreateInitialTodos
ãšããé¢æ°ããèªäœãæž¡ããŠããããšã«æ³šæããŠãã ãããuseState
ã«é¢æ°ãæž¡ãããå ŽåãReact ã¯åæåæã«ãé¢æ°ãäžåºŠã ãåŒã³åºããŸãã
åæåé¢æ°ãçŽç²ã§ããããšã確èªãããããéçºæã«åæåé¢æ°ã 2 ååŒã³åºãããããšããããŸãã
äŸ 1/2: åæåé¢æ°ãæž¡ã
ãã®äŸã§ã¯ãåæåé¢æ°ãå©çšããŠããŸãããã®ãããcreateInitialTodos
é¢æ°ã¯åæåæã®ã¿å®è¡ãããŸããå
¥åãã£ãŒã«ãã«æåãå
¥åããå Žåãªã©ã®ãã³ã³ããŒãã³ãã®åã¬ã³ããŒæã«ã¯å®è¡ãããŸããã
import { useState } from 'react'; function createInitialTodos() { const initialTodos = []; for (let i = 0; i < 50; i++) { initialTodos.push({ id: i, text: 'Item ' + (i + 1) }); } return initialTodos; } export default function TodoList() { const [todos, setTodos] = useState(createInitialTodos); const [text, setText] = useState(''); return ( <> <input value={text} onChange={e => setText(e.target.value)} /> <button onClick={() => { setText(''); setTodos([{ id: todos.length, text: text }, ...todos]); }}>Add</button> <ul> {todos.map(item => ( <li key={item.id}> {item.text} </li> ))} </ul> </> ); }
key ãå©çšã㊠state ããªã»ãããã
key
å±æ§ã¯ããªã¹ããã¬ã³ããŒããå Žåã«ããå©çšããŸãããããããã 1 ã€ã®äœ¿ãéããããŸãã
ã³ã³ããŒãã³ãã«ç°ãªã key
ãæž¡ãããšã§ãã³ã³ããŒãã³ãã® state ããªã»ããããããšãã§ããŸãããã®äŸã§ã¯ãversion
state å€æ°ã Form
ã« key
ãšããŠæž¡ããŠããŸããâResetâ ãã¿ã³ãã¯ãªãã¯ãããšãversion
state å€æ°ãå€åããŸããkey
ãå€åãããšããReact 㯠Form
ã³ã³ããŒãã³ãïŒãšããã®å
šãŠã®åã³ã³ããŒãã³ãïŒãäžããåçæãããããForm
ã® state ããªã»ãããããŸãã
詳ããã¯ãstate ã®ä¿æãšãªã»ãããåç §ããŠãã ããã
import { useState } from 'react'; export default function App() { const [version, setVersion] = useState(0); function handleReset() { setVersion(version + 1); } return ( <> <button onClick={handleReset}>Reset</button> <Form key={version} /> </> ); } function Form() { const [name, setName] = useState('Taylor'); return ( <> <input value={name} onChange={e => setName(e.target.value)} /> <p>Hello, {name}.</p> </> ); }
çŽåã®ã¬ã³ããŒã®æ å ±ãä¿åãã
éåžžãstate ã®æŽæ°ã¯ã€ãã³ããã³ãã©ã®äžã§è¡ãããŸããããããã¬ã³ããŒã«å¿ã㊠state ãèšå®ãããå ŽåããããŸããäŸãã°ãprop ãå€åãããšãã« state å€æ°ãå€åããããå Žåã§ãã
以äžã«ç€ºãããã«ãã»ãšãã©ã®ã±ãŒã¹ã§ã¯äžèŠã§ãã
- ããå¿
èŠãªå€ãçŸåšã® props ãšä»ã® state ã®ã¿ããå°åºãããå Žåãåé·ãª state ãåé€ããŠãã ãããããäœåºŠãåèšç®ãããããšãæ°ã«ãªãå Žåã¯ã
useMemo
ããã¯ã圹ã«ç«ã¡ãŸãã - ããã³ã³ããŒãã³ãããªãŒã® state å
šäœããªã»ãããããå Žåãã³ã³ããŒãã³ãã«ç°ãªã
key
ãæž¡ããŠãã ããã - å¯èœã§ããã°ãé¢é£ãããã¹ãŠã® state ãã€ãã³ããã³ãã©ã®äžã§æŽæ°ããŠãã ããã
ããããã©ããé©çšã§ããªãçšãªã±ãŒã¹ã§ã¯ãã³ã³ããŒãã³ãã®ã¬ã³ããŒäžã« set
é¢æ°ãåŒã³åºãããããŸã§ã«ã¬ã³ããŒãããå€ã«åºã¥ã㊠state ãæŽæ°ãããã¿ãŒã³ãå©çšã§ããŸãã
以äžã®äŸã§ã¯ãCountLabel
ã³ã³ããŒãã³ãã¯ãæž¡ããã count
ããããã£ã衚瀺ããŠããŸãã
export default function CountLabel({ count }) {
return <h1>{count}</h1>
}
çŽè¿ã®å€æŽã§ãcounter ã®å€ãå¢ããã®ãæžã£ãã®ãã衚瀺ããããšããŸããcount
ããããã£ã ãã§ã¯ç¥ãããšãã§ããªããããååã®å€ãä¿æãç¶ããå¿
èŠããããŸããååã®å€ãä¿æããããã«ãprevCount
state å€æ°ãè¿œå ããŸããããã«ãtrend
state å€æ°ãè¿œå ããcount ãå¢ããã®ãæžã£ãã®ããä¿æããŸããprevCount
ãš count
ãæ¯èŒããããããããäžèŽããªãå Žåã«ãprevCount
ãš trend
ãæŽæ°ããŸããããã§ãçŸåšã® count ããããã£ãšãååã®ã¬ã³ããŒããã©ã®ããã«å€åããã®ãã®äž¡æ¹ã衚瀺ããããšãã§ããŸãã
import { useState } from 'react'; export default function CountLabel({ count }) { const [prevCount, setPrevCount] = useState(count); const [trend, setTrend] = useState(null); if (prevCount !== count) { setPrevCount(count); setTrend(count > prevCount ? 'increasing' : 'decreasing'); } return ( <> <h1>{count}</h1> {trend && <p>The count is {trend}</p>} </> ); }
ã¬ã³ããŒäžã« set
é¢æ°ãåŒã³åºãå Žåã¯ãprevCount !== count
ã®ãããªæ¡ä»¶ç¯ã®äžã§ãsetPrevCount(count)
ã®ãããªåŒã³åºããå¿
èŠãªããšã«æ³šæããŠãã ããããããªããšãåã¬ã³ããŒã®ã«ãŒãã«é¥ããã³ã³ããŒãã³ããã¯ã©ãã·ã¥ããŸãããŸããäŸã®ããã«ãçŸåšã¬ã³ããŒããŠããã³ã³ããŒãã³ãã® state ã®ã¿æŽæ°ããããšãã§ããŸããã¬ã³ããŒäžã«å¥ã®ã³ã³ããŒãã³ãã® set
é¢æ°ãåŒã³åºããšãšã©ãŒã«ãªããŸããæåŸã«ãset
é¢æ°ã®åŒã³åºãã¯ãæžãæããªã㧠state ãæŽæ°ããå¿
èŠããããŸããããã¯ãçŽé¢æ°ã®ä»ã®ã«ãŒã«ãç Žãããšãã§ããªãããšãæå³ããŸãã
ãã®ãã¿ãŒã³ã¯ç解ããã®ãé£ãããããéåžžã¯é¿ããã¹ãã§ãããããããšãã§ã¯ãå
㧠state ãæŽæ°ããããã¯è¯ãæ¹æ³ã§ããã¬ã³ããŒäžã« set
é¢æ°ãåŒã³åºããšãã³ã³ããŒãã³ãã return
æã§çµäºããçŽåŸãåã³ã³ããŒãã³ããã¬ã³ããŒããåã«åã¬ã³ããŒãè¡ãããŸãããã®ãããåã³ã³ããŒãã³ãã 2 åã¬ã³ããŒãããã«æžã¿ãŸããã³ã³ããŒãã³ãé¢æ°ã®æ®ãã®éšåã¯åŒãç¶ãå®è¡ãããŸãïŒçµæã¯ç Žæ£ãããŸããïŒããããset
é¢æ°ã®åŒã³åºããå«ãæ¡ä»¶åå²ããå
šãŠã®ããã¯ã®åŒã³åºãããäžã«ããå Žåãæ©æ return;
ãè¿œå ããŠãåã¬ã³ããŒãæ©ããããšãã§ããŸãã
ãã©ãã«ã·ã¥ãŒãã£ã³ã°
state ãæŽæ°ããã®ã«å€ãå€ããã°ã«è¡šç€ºããã
set
é¢æ°ã®åŒã³åºãã¯ãå®è¡äžã®ã³ãŒãã® state ãå€åãããŸããã
function handleClick() {
console.log(count); // 0
setCount(count + 1); // Request a re-render with 1
console.log(count); // Still 0!
setTimeout(() => {
console.log(count); // Also 0!
}, 5000);
}
ããã¯ãstate ãã¹ãããã·ã§ããã®ããã«æ¯ãèãããã§ããstate ã®æŽæ°ã¯ãæ°ãã state ã®å€ã§ã®åã¬ã³ããŒããªã¯ãšã¹ãããŸãããã§ã«å®è¡äžã®ã€ãã³ããã³ãã©å
ã® count
ãšãã JavaScript å€æ°ã«ã¯åœ±é¿ãäžããŸããã
次㮠state ãå¿
èŠãªå Žåã¯ãset
é¢æ°ã«æž¡ãåã«äžåºŠå€æ°ã«ä¿åããããšãã§ããŸãã
const nextCount = count + 1;
setCount(nextCount);
console.log(count); // 0
console.log(nextCount); // 1
state ãæŽæ°ããã®ã«ç»é¢ãæŽæ°ãããªã
React ã§ã¯ãæŽæ°ã®ååŸã§ state ã®å€ãå€åããªãå Žåããã®å€æŽã¯ç¡èŠãããŸããstate ã®å€ã®å€åã¯ãObject.is
ã«ãã£ãŠå€æãããŸãããã®çŸè±¡ã¯ãstate ã®ãªããžã§ã¯ããé
åãçŽæ¥æžãæããå Žåã«ããèµ·ãããŸãã
obj.x = 10; // ð© Wrong: mutating existing object
setObj(obj); // ð© Doesn't do anything
æ¢åã® obj
ãªããžã§ã¯ããæžãæããŠãsetObj
ã«æ»ããããããã®æŽæ°ã¯ç¡èŠãããŸããä¿®æ£ããã«ã¯ãstate ã®ãªããžã§ã¯ããé
åãæžãæããã®ã§ã¯ãªãã眮ãæããå¿
èŠããããŸãã
// â
Correct: creating a new object
setObj({
...obj,
x: 10
});
âToo many re-rendersâ ãšãããšã©ãŒãåºã
Too many re-renders. React limits the number of renders to prevent an infinite loop.
ãšãããšã©ãŒãåºãããšããããŸããããã¯éåžžãã¬ã³ããŒäžã«ç¡æ¡ä»¶ã« set
é¢æ°ãåŒã³åºããŠãããããã³ã³ããŒãã³ããã«ãŒãã«å
¥ã£ãŠããããšãæå³ããŸããã¬ã³ããŒãset
é¢æ°ã®åŒã³åºãïŒã¬ã³ããŒãåŒãèµ·ããïŒãã¬ã³ããŒãset
é¢æ°ã®åŒã³åºãïŒã¬ã³ããŒãåŒãèµ·ããïŒããšããããã«ç¶ããŸãã倧æµã®å Žåãããã¯ã€ãã³ããã³ãã©ã®æå®ãééã£ãããšã«ãããã®ã§ãã
// ð© Wrong: calls the handler during render
return <button onClick={handleClick()}>Click me</button>
// â
Correct: passes down the event handler
return <button onClick={handleClick}>Click me</button>
// â
Correct: passes down an inline function
return <button onClick={(e) => handleClick(e)}>Click me</button>
ãã®ãšã©ãŒã®åå ãããããªãå Žåã¯ãã³ã³ãœãŒã«ã®ãšã©ãŒã®æšªã«ããç¢å°ãã¯ãªãã¯ããŠãJavaScript ã¹ã¿ãã¯ã調ã¹ããšã©ãŒã®åå ãšãªã set
é¢æ°ã®åŒã³åºããç¹å®ããŠãã ããã
åæåé¢æ°ãæŽæ°çšé¢æ°ã 2 床åŒã°ãã
Strict Mode ã§ã¯ãããã€ãã®é¢æ°ããæ¬æ¥ 1 åã®ãšããã 2 ååŒã³åºãããããšããããŸãã
function TodoList() {
// This component function will run twice for every render.
const [todos, setTodos] = useState(() => {
// This initializer function will run twice during initialization.
return createTodos();
});
function handleClick() {
setTodos(prevTodos => {
// This updater function will run twice for every click.
return [...prevTodos, createTodo()];
});
}
// ...
ããã¯äºæ³ãããåäœã§ãããããªãã®ã³ãŒããå£ããã®ã§ã¯ãããŸããã
ããã¯éçºæã®ã¿ã®æåã§ãã³ã³ããŒãã³ããçŽç²ã«ä¿ã€ããã«åœ¹ç«ã¡ãŸããReact ã¯ãåŒã³åºãçµæã® 1 ã€ãå©çšãããã 1 ã€ãç¡èŠããŸããã³ã³ããŒãã³ããåæåé¢æ°ãæŽæ°çšé¢æ°ãçŽç²ã§ããã°ããã®æåãããªãã®ããžãã¯ã«åœ±é¿ãäžããããšã¯ãããŸããããã ãã誀ã£ãŠçŽç²ã§ãªãé¢æ°ãæå®ããå Žåã¯ãããã«ããééãã«æ°ä»ãããšãã§ããã§ãããã
äŸãã°ä»¥äžã®æŽæ°çšé¢æ°ã¯ãstate ã®é åãæžãæããããçŽç²ã§ã¯ãããŸããã
setTodos(prevTodos => {
// ð© Mistake: mutating state
prevTodos.push(createTodo());
});
React ã¯æŽæ°çšé¢æ°ã 2 ååŒã³åºããããtodo ã 2 ã€è¿œå ãããŠããŸããééãã«æ°ä»ãããšãã§ããŸãããã®äŸã§ã¯ãé åãæžãæããã®ã§ã¯ãªãã眮ãæããããšã§ééããä¿®æ£ã§ããŸãã
setTodos(prevTodos => {
// â
Correct: replacing with new state
return [...prevTodos, createTodo()];
});
æŽæ°çšé¢æ°ãçŽç²ã«ãªã£ããããè€æ°ååŒã³åºãããŠãåäœã«åœ±é¿ããŸããããããã2 ååŒã³åºãããããšã§ééãã«æ°ä»ãããšãã§ããçç±ã§ããã³ã³ããŒãã³ããåæåé¢æ°ãæŽæ°çšé¢æ°ã®ã¿ãçŽç²ã§ããå¿ èŠããããŸããã€ãã³ããã³ãã©ã¯ãçŽç²ã§ããå¿ èŠããªãããã2 ååŒã³åºãããããšã¯ãããŸããã
詳ããã¯ãã³ã³ããŒãã³ããçŽç²ã«ä¿ã€ãåç §ããŠãã ããã
é¢æ°ã state ã«ã»ããããããšãããšããã®é¢æ°ãåŒã³åºãããŠããŸã
ãã®ãããªåœ¢ã§é¢æ°ã state ã«èšå®ããããšã¯ã§ããŸããã
const [fn, setFn] = useState(someFunction);
function handleClick() {
setFn(someOtherFunction);
}
é¢æ°ãæž¡ããšãReact 㯠someFunction
ãåæåé¢æ°ãsomeOtherFunction
ãæŽæ°çšé¢æ°ãšããŠæ±ããŸãããã®ãããããããåŒã³åºãããã®çµæãä¿åããããšããŸããé¢æ°ãå®è¡ããã®ã§ã¯ãªãä¿åããã«ã¯ãã©ã¡ãã®å Žåã () =>
ãåã«ä»ããå¿
èŠããããŸãããããããšãReact ã¯é¢æ°èªäœãä¿åããŸãã
const [fn, setFn] = useState(() => someFunction);
function handleClick() {
setFn(() => someOtherFunction);
}