usereducerフック
26586 ワード
o減速剤?
還元的なNada Maisは、que - uma - funの上で、エストラーヌe馬を非難します.
(state, action) => newState; // Assinatura de um reducer
<研究ノート>ガーゼの錯綜性についてVAMOSは、Ar - ent Ent - o o com umについて説明します.
リストを行う
const initialTodos = [
{
id: 1,
title: "Tarefa 1",
complete: false,
},
{
id: 2,
title: "Tarefa 2",
complete: false,
},
];
function Todos() {
const [todos, setTodos] = useState(initialTodos);
const handleComplete = (todo) => {
const { id } = todo;
const updatedTodos = todos.map((t) => {
if (t.id === id) {
return { ...t, complete: !todo.complete };
}
return t;
});
setTodos(updatedTodos);
};
return (
<>
{todos.map((todo) => (
<div key={todo.id}>
<label>
<input
type="checkbox"
checked={todo.complete}
onChange={() => handleComplete(todo)}
/>
{todo.title}
</label>
</div>
))}
</>
);
}
ReactDOM.render(<Todos />, document.getElementById("root"));
エッセCは、Fociona、PorがmエスタモスAribuindo A lのGiAデMudanを形づくってください.クレッシェンドのためのNecso Estadoは、oを結びつけます、そして、エサlは、gica ficarを食べます.ペパデロパラシュートで降下する人Qualten Manontenは、去勢します!
コモソルucionamos isso?Expenamente o que penso:um還元剤.El Vai Concentrar Toda A l ' s Gica de Mudanはde esteste e em nosso component ens s o o necess und riexextir .vamos l .
救助へのユーザ教育
vamos usar oフック
useReducer
. comエレpodemosクリアーtoda nossa lは、gicaデestado em um sをlul eの「disparええ- LA」(派遣)com a a a ' s es(行動).Dosso Cは、digo alteradoをesseします.
const initialTodos = [
{
id: 1,
title: "Tarefa 1",
complete: false,
},
{
id: 2,
title: "Tarefa 2",
complete: false,
},
];
const reducer = (state, action) => {
switch (action.type) {
case "COMPLETE":
return state.map((todo) => {
if (todo.id === action.id) {
return { ...todo, complete: !todo.complete };
}
return todo;
});
default:
return state;
}
};
function Todos() {
const [todos, dispatch] = useReducer(reducer, initialTodos);
const handleComplete = (todo) => {
const { id } = todo;
dispatch({ type: "COMPLETE", id });
};
return (
<>
{todos.map((todo) => (
<div key={todo.id}>
<label>
<input
type="checkbox"
checked={todo.complete}
onChange={() => handleComplete(todo)}
/>
{todo.title}
</label>
</div>
))}
</>
);
}
ReactDOM.render(<Todos />, document.getElementById("root"));
Pulcoは、aを支持します?<研究報告>😊state
エステド・アリアaction
(エグゼクティブ版)state
インバラード.『完全な』、パーコリズス・アルゴンス・パソスにとってのクァンド・オ・ティポ
todo.id === action.id
). { ...todo }
), コピアモスのオスヴァレレスとN . C .complete
. com o還元剤モンタロ、代理人o o o
useState
PORuseReducer
, Passando o PRは、prio還元器e o estestadoを接待します.Assim como
useState
,useReducer
retorna um array e podemos usar destructuring para atribuir esse retorno em consts, no nosso caso,todos
(a lista de tarefas, ou estado atual) edispatch
(função para mudar o estado).
handleComplete
) アゴラsディスプリズマモスディスカバリー.還元剤還元剤
vamos agora incrementar nosso reducer permitindo ao usuはrioの挿入物eの排他的である.Isso vai mostrar comoはmais f mre fのmanter tudo em um s s lul .
Primeiro o cは、digoコンプリート、E EUは、Altera Ses - es Eesロゴem Segiaとして説明します.
const initialTodos = [
{
id: 1,
title: "Tarefa 1",
complete: false,
},
{
id: 2,
title: "Tarefa 2",
complete: false,
},
];
const reducer = (state, action) => {
switch (action.type) {
case "COMPLETE":
return state.map((todo) => {
if (todo.id === action.id) {
return { ...todo, complete: !todo.complete };
}
return todo;
});
case "INSERT":
return state.concat({
id: Math.random(),
title: action.title,
complete: false,
});
case "DELETE":
return state.filter((todo) => {
return todo.id !== action.id;
});
default:
return state;
}
};
function Todos() {
const [todo, setTodo] = useState("");
const [todos, dispatch] = useReducer(reducer, initialTodos);
const handleComplete = (todo) => {
const { id } = todo;
dispatch({ type: "COMPLETE", id });
};
const handleInsert = (e) => {
e.preventDefault();
dispatch({ type: "INSERT", title: todo });
};
const handleDelete = (e, todo) => {
e.preventDefault();
dispatch({ type: "DELETE", id: todo.id });
};
return (
<>
<label>
Incluir item
<input value={todo} onChange={(e) => setTodo(e.target.value)} />
<button onClick={handleInsert}>Inserir</button>
</label>
{todos.map((todo) => (
<div key={todo.id}>
<label>
<input
type="checkbox"
checked={todo.complete}
onChange={() => handleComplete(todo)}
/>
{todo.title}
<button onClick={(e) => handleDelete(e, todo)}>X</button>
</label>
</div>
))}
</>
);
}
ReactDOM.render(<Todos />, document.getElementById("root"));
Nenhumグランデ偏析concat
para incluir um novo item no array.filter
<高橋潤子>todo.id !== action.id
) 結論
クワッドノッソエストラドou l ' gicaパラシュートで降下する
useReducer
Pia Manter A l Giy gica num .ニコローカル.ルポ・メッカラー
useReducer
コムuseState
OUTROsフックのcomo fizemos no nossoの例題useContext
( Basicamente para Compartihar Estados , Componentes , Mas Isoso abordaremos em outro artigo )についてエスペコqueエッセアーガゴテンハアダドゥードは、エンターラーmelhor o que
useReducer
.NOS VemosなしPRは、Ximo artigoをします.🤓
Reference
この問題について(usereducerフック), 我々は、より多くの情報をここで見つけました https://dev.to/daniloraisi/usereducer-hook-1fpcテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol