{"id":3701,"date":"2024-11-10T17:16:07","date_gmt":"2024-11-10T09:16:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3701\/"},"modified":"2024-11-10T17:16:07","modified_gmt":"2024-11-10T09:16:07","slug":"usereducer%ef%bc%9areact-hooks","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3701\/","title":{"rendered":"useReducer\uff1aReact Hooks"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173112160367528.jpg\" class=\"aligncenter\" title=\"useReducer\uff1aReact Hooks\u63d2\u56fe\" alt=\"useReducer\uff1aReact Hooks\u63d2\u56fe\" \/><\/p>\n<h2> react \u4e2d\u7684 usereducer\uff1a\u901a\u8fc7\u4e24\u4e2a\u8ff7\u4f60\u9879\u76ee\u7b80\u5316\u72b6\u6001\u7ba1\u7406 <\/h2>\n<h2> \u4ecb\u7ecd <\/h2>\n<p>\u72b6\u6001\u7ba1\u7406\u662f\u5728 react \u4e2d\u6784\u5efa\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u90e8\u5206\u3002\u867d\u7136 usestate \u8db3\u4ee5\u7ba1\u7406\u7b80\u5355\u72b6\u6001\uff0c\u4f46\u968f\u7740\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0cusereducer \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u5f3a\u5927\u3001\u53ef\u9884\u6d4b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5b83\u3002\u53d7 redux \u7684\u51cf\u901f\u5668\u6a21\u5f0f\u7684\u542f\u53d1\uff0cusereducer \u5141\u8bb8\u60a8\u5b9a\u4e49\u72b6\u6001\u8f6c\u6362\u5e94\u5982\u4f55\u53d1\u751f\u4ee5\u54cd\u5e94\u7279\u5b9a\u64cd\u4f5c\uff0c\u4f7f\u5176\u6210\u4e3a\u5177\u6709\u591a\u4e2a\u590d\u6742\u72b6\u6001\u66f4\u65b0\u7684\u573a\u666f\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\uff1a<\/p>\n<ol>\n<li>\u8be6\u7ec6\u89e3\u91ca usereducer\u3001\u5176\u8bed\u6cd5\u4ee5\u53ca\u4f55\u65f6\u4f7f\u7528\u5b83\u3002<\/li>\n<li>\u5b9e\u65bd\u4e24\u4e2a\u5c0f\u9879\u76ee\uff1a\n<ul>\n<li> <strong>\u5177\u6709\u591a\u4e2a\u64cd\u4f5c\u7684\u8ba1\u6570\u5668<\/strong>\uff1a\u8d85\u8d8a\u57fa\u672c\u589e\u91cf\/\u51cf\u91cf\u7684\u793a\u4f8b\uff0c\u5c55\u793a usereducer \u5982\u4f55\u5904\u7406\u591a\u4e2a\u64cd\u4f5c\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u5177\u6709\u590d\u6742\u72b6\u6001\u8f6c\u6362\u7684\u5f85\u529e\u4e8b\u9879\u5217\u8868<\/strong>\uff1a\u4e00\u6b3e\u7a81\u51fa usereducer \u7ba1\u7406\u590d\u6742\u72b6\u6001\u5bf9\u8c61\u80fd\u529b\u7684\u5f85\u529e\u4e8b\u9879\u5e94\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u8ba9\u6211\u4eec\u6df1\u5165\u4e86\u89e3 usereducer \u5982\u4f55\u7b80\u5316 react \u4e2d\u7684\u72b6\u6001\u7ba1\u7406\uff01<\/p>\n<hr>\n<h2> \u7406\u89e3 usereducer <\/h2>\n<h3> \u4ec0\u4e48\u662f usereducer\uff1f <\/h3>\n<p>usereducer \u662f\u4e00\u4e2a react hook\uff0c\u4e13\u4e3a usestate \u4e0d\u8db3\u7684\u60c5\u51b5\u800c\u8bbe\u8ba1\u3002\u60a8\u4e0d\u662f\u76f4\u63a5\u66f4\u65b0\u72b6\u6001\uff0c\u800c\u662f\u6307\u5b9a\u4e00\u4e2a\u6839\u636e\u5f53\u524d\u72b6\u6001\u548c\u64cd\u4f5c\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u72b6\u6001\u7684\u51cf\u901f\u5668\u51fd\u6570\u3002\u8fd9\u79cd\u58f0\u660e\u5f0f\u65b9\u6cd5\u4f7f\u72b6\u6001\u8f6c\u6362\u4fdd\u6301\u53ef\u9884\u6d4b\uff0c\u5e76\u5141\u8bb8\u60a8\u4ee5\u96c6\u4e2d\u7684\u65b9\u5f0f\u7ba1\u7406\u66f4\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u3002<\/p>\n<h3> usereducer \u7684\u8bed\u6cd5 <\/h3>\n<p>\u4ee5\u4e0b\u662f\u8bed\u6cd5\u7ec6\u5206\uff1a<\/p>\n<pre>const [state, dispatch] = usereducer(reducer, initialstate);\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>\n<p><strong>reducer<\/strong>\uff1a\u5b9a\u4e49\u5982\u4f55\u6839\u636e\u64cd\u4f5c\u66f4\u65b0\u72b6\u6001\u7684\u51fd\u6570\u3002\u5b83\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n<ul>\n<li> state\uff1a\u5f53\u524d\u72b6\u6001\u3002<\/li>\n<li> action\uff1a\u5305\u542b\u6709\u5173\u64cd\u4f5c\u4fe1\u606f\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u5305\u62ec\u7c7b\u578b\u548c\u53ef\u9009\u7684\u6709\u6548\u8d1f\u8f7d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>initialstate<\/strong>\uff1a\u72b6\u6001\u7684\u8d77\u59cb\u503c\u3002<\/p>\n<\/li>\n<\/ul>\n<h3> \u793a\u4f8b\uff1a\u4f7f\u7528 usereducer \u7684\u57fa\u672c\u8ba1\u6570\u5668 <\/h3>\n<p>\u8ba9\u6211\u4eec\u4f7f\u7528 usereducer \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u6570\u5668\u6765\u67e5\u770b\u5b9e\u9645\u8bed\u6cd5\u3002<\/p>\n<pre>import react, { usereducer } from 'react';\n\nfunction reducer(state, action) {\n    switch (action.type) {\n        case 'increment':\n            return { count: state.count + 1 };\n        case 'decrement':\n            return { count: state.count - 1 };\n        default:\n            return state;\n    }\n}\n\nfunction counter() {\n    const [state, dispatch] = usereducer(reducer, { count: 0 });\n\n    return (\n        &lt;div&gt;\n            &lt;p&gt;count: {state.count}&lt;\/p&gt;\n            &lt;button onclick={() =&gt; dispatch({ type: 'increment' })}&gt;increment&lt;\/button&gt;\n            &lt;button onclick={() =&gt; dispatch({ type: 'decrement' })}&gt;decrement&lt;\/button&gt;\n        &lt;\/div&gt;\n    );\n}\n\nexport default counter;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3> \u5b88\u5219\u89e3\u91ca <\/h3>\n<ol>\n<li> <strong>reducer \u51fd\u6570<\/strong>\uff1a\u8be5\u51fd\u6570\u5b9a\u4e49\u5982\u4f55\u5904\u7406\u52a8\u4f5c\u3002\u6839\u636e\u64cd\u4f5c\u7c7b\u578b\uff08\u9012\u589e\u6216\u9012\u51cf\uff09\uff0creducer \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u72b6\u6001\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u8c03\u5ea6\u64cd\u4f5c<\/strong>\uff1a\u8c03\u5ea6\u5c06\u4e00\u4e2a\u64cd\u4f5c\u53d1\u9001\u5230reducer\uff0creducer\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u5e76\u76f8\u5e94\u5730\u66f4\u65b0\u72b6\u6001\u3002<\/li>\n<\/ol>\n<hr>\n<h2> \u4f55\u65f6\u4f7f\u7528 usereducer <\/h2>\n<p>usereducer \u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u7279\u522b\u6709\u7528\uff1a<\/p>\n<ul>\n<li>\u72b6\u6001\u903b\u8f91\u590d\u6742\u6216\u6d89\u53ca\u591a\u4e2a\u5b50\u503c\u3002<\/li>\n<li>\u4e0b\u4e00\u4e2a\u72b6\u6001\u53d6\u51b3\u4e8e\u524d\u4e00\u4e2a\u72b6\u6001\u3002<\/li>\n<li>\u591a\u4e2a\u7ec4\u4ef6\u9700\u8981\u8bbf\u95eereducer\u7ba1\u7406\u7684\u72b6\u6001\uff08\u53ef\u4ee5\u5c06usereducer\u548cusecontext\u7ed3\u5408\u8d77\u6765\u4f7f\u7528\u5168\u5c40\u72b6\u6001\uff09\u3002<\/li>\n<\/ul>\n<hr>\n<h2> \u8ff7\u4f60\u9879\u76ee1\uff1a\u591a\u4e2a\u52a8\u4f5c\u7684\u8ba1\u6570\u5668 <\/h2>\n<p>\u5728\u8fd9\u4e2a\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u589e\u5f3a\u7684\u8ba1\u6570\u5668\uff0c\u5141\u8bb8\u591a\u79cd\u64cd\u4f5c\uff08\u9012\u589e\u3001\u9012\u51cf\u3001\u91cd\u7f6e\uff09\uff0c\u4ee5\u4e86\u89e3 usereducer \u5982\u4f55\u5904\u7406\u66f4\u5e7f\u6cdb\u7684\u64cd\u4f5c\u3002<\/p>\n<h3> \u6b65\u9aa4\u4e00\uff1a\u5b9a\u4e49reducer\u51fd\u6570 <\/h3>\n<pre>import react, { usereducer } from 'react';\n\nfunction reducer(state, action) {\n    switch (action.type) {\n        case 'increment':\n            return { count: state.count + 1 };\n        case 'decrement':\n            return { count: state.count - 1 };\n        case 'reset':\n            return { count: 0 };\n        default:\n            throw new error(`unknown action: ${action.type}`);\n    }\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3> \u7b2c2\u6b65\uff1a\u521b\u5efa\u8ba1\u6570\u5668\u7ec4\u4ef6 <\/h3>\n<pre>function enhancedcounter() {\n    const [state, dispatch] = usereducer(reducer, { count: 0 });\n\n    return (\n        &lt;div&gt;\n            &lt;p&gt;count: {state.count}&lt;\/p&gt;\n            &lt;button onclick={() =&gt; dispatch({ type: 'increment' })}&gt;increment&lt;\/button&gt;\n            &lt;button onclick={() =&gt; dispatch({ type: 'decrement' })}&gt;decrement&lt;\/button&gt;\n            &lt;button onclick={() =&gt; dispatch({ type: 'reset' })}&gt;reset&lt;\/button&gt;\n        &lt;\/div&gt;\n    );\n}\n\nexport default enhancedcounter;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u9664\u4e86\u9012\u589e\u548c\u9012\u51cf\u4e4b\u5916\uff0c\u6b64\u589e\u5f3a\u578b\u8ba1\u6570\u5668\u73b0\u5728\u8fd8\u652f\u6301\u91cd\u7f6e\u529f\u80fd\u3002\u8be5\u9879\u76ee\u5c55\u793a\u4e86 usereducer \u5728\u7ba1\u7406\u72b6\u6001\u66f4\u65b0\u64cd\u4f5c\u65b9\u9762\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<hr>\n<h2> \u8ff7\u4f60\u9879\u76ee 2\uff1a\u6784\u5efa\u5177\u6709\u590d\u6742\u72b6\u6001\u8f6c\u6362\u7684\u5f85\u529e\u4e8b\u9879\u5217\u8868 <\/h2>\n<p>\u5f85\u529e\u4e8b\u9879\u5217\u8868\u5e94\u7528\u7a0b\u5e8f\u5f3a\u8c03\u4e86 usereducer \u5982\u4f55\u975e\u5e38\u9002\u5408\u7ba1\u7406\u5177\u6709\u591a\u4e2a\u8f6c\u6362\u7684\u590d\u6742\u72b6\u6001\u5bf9\u8c61\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u548c\u5207\u6362\u4efb\u52a1\u3002<\/p>\n<h3> \u6b65\u9aa4\u4e00\uff1a\u5b9a\u4e49reducer <\/h3>\n<pre>function todoreducer(state, action) {\n    switch (action.type) {\n        case 'add':\n            return [...state, { id: date.now(), text: action.payload, completed: false }];\n        case 'remove':\n            return state.filter(todo =&gt; todo.id !== action.payload);\n        case 'toggle':\n            return state.map(todo =&gt;\n                todo.id === action.payload ? { ...todo, completed: !todo.completed } : todo\n            );\n        default:\n            throw new error(`unknown action type: ${action.type}`);\n    }\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3> \u7b2c 2 \u6b65\uff1a\u521b\u5efa\u5f85\u529e\u4e8b\u9879\u5217\u8868\u7ec4\u4ef6 <\/h3>\n<pre>import React, { useReducer, useState } from 'react';\n\nfunction ToDoList() {\n    const [todos, dispatch] = useReducer(todoReducer, []);\n    const [task, setTask] = useState('');\n\n    const handleAdd = () =&gt; {\n        if (task.trim()) {\n            dispatch({ type: 'add', payload: task });\n            setTask(''); \/\/ Clear input field\n        }\n    };\n\n    return (\n        &lt;div&gt;\n            &lt;h2&gt;To-Do List&lt;\/h2&gt;\n            &lt;input\n                value={task}\n                onChange={e =&gt; setTask(e.target.value)}\n                placeholder=\"Enter a new task\"\n            \/&gt;\n            &lt;button onClick={handleAdd}&gt;Add Task&lt;\/button&gt;\n\n            &lt;ul&gt;\n                {todos.map(todo =&gt; (\n                    &lt;li key={todo.id} style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}&gt;\n                        {todo.text}\n                        &lt;button onClick={() =&gt; dispatch({ type: 'toggle', payload: todo.id })}&gt;\n                            {todo.completed ? 'Undo' : 'Complete'}\n                        &lt;\/button&gt;\n                        &lt;button onClick={() =&gt; dispatch({ type: 'remove', payload: todo.id })}&gt;Delete&lt;\/button&gt;\n                    &lt;\/li&gt;\n                ))}\n            &lt;\/ul&gt;\n        &lt;\/div&gt;\n    );\n}\n\nexport default ToDoList;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3> \u5f85\u529e\u4e8b\u9879\u5217\u8868\u4ee3\u7801\u8bf4\u660e <\/h3>\n<ol>\n<li>\n<p><strong>\u884c\u52a8<\/strong>\uff1a<\/p>\n<ul>\n<li> <strong>\u6dfb\u52a0<\/strong>\uff1a\u5c06\u65b0\u4efb\u52a1\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u5e76\u4f7f\u7528\u552f\u4e00 id \u5e76\u5c06\u5df2\u5b8c\u6210\u72b6\u6001\u8bbe\u7f6e\u4e3a false\u3002<\/li>\n<li> <strong>\u5220\u9664<\/strong>\uff1a\u6839\u636eid\u8fc7\u6ee4\u6389\u4efb\u52a1\u6765\u5220\u9664\u5b83\u3002<\/li>\n<li> <strong>\u5207\u6362<\/strong>\uff1a\u901a\u8fc7\u5207\u6362\u5df2\u5b8c\u6210\u72b6\u6001\u5c06\u4efb\u52a1\u6807\u8bb0\u4e3a\u5df2\u5b8c\u6210\u6216\u672a\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5c06 usereducer \u4e0e\u52a8\u6001\u6570\u636e\u7ed3\u5408\u4f7f\u7528<\/strong>\uff1a\u6b64\u793a\u4f8b\u5c55\u793a\u4e86 usereducer \u5982\u4f55\u5904\u7406\u5bf9\u8c61\u6570\u7ec4\u4e2d\u590d\u6742\u7684\u5d4c\u5957\u72b6\u6001\u66f4\u65b0\uff0c\u4f7f\u5176\u975e\u5e38\u9002\u5408\u7ba1\u7406\u5177\u6709\u591a\u4e2a\u5c5e\u6027\u7684\u9879\u76ee\u3002<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u60a8\u5b66\u4e60\u4e86\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528 usereducer \u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u8fdb\u884c\u66f4\u590d\u6742\u7684\u72b6\u6001\u7ba1\u7406\u3002\u901a\u8fc7\u6211\u4eec\u7684\u9879\u76ee\uff1a<\/p>\n<ol>\n<li> <strong>\u589e\u5f3a\u578b\u8ba1\u6570\u5668<\/strong>\u6f14\u793a\u4e86 usereducer \u5982\u4f55\u7b80\u5316\u591a\u4e2a\u57fa\u4e8e\u64cd\u4f5c\u7684\u72b6\u6001\u66f4\u65b0\u3002<\/li>\n<li> <strong>\u5f85\u529e\u4e8b\u9879\u5217\u8868<\/strong>\u8bf4\u660e\u4e86\u5982\u4f55\u4f7f\u7528 usereducer \u7ba1\u7406\u590d\u6742\u7684\u72b6\u6001\u5bf9\u8c61\uff0c\u4f8b\u5982\u4efb\u52a1\u6570\u7ec4\u3002<\/li>\n<\/ol>\n<p>\u4f7f\u7528 usereducer\uff0c\u60a8\u53ef\u4ee5\u4e3a\u9700\u8981\u5f3a\u5927\u72b6\u6001\u7ba1\u7406\u7684\u5e94\u7528\u7a0b\u5e8f\u7f16\u5199\u66f4\u5e72\u51c0\u3001\u66f4\u53ef\u9884\u6d4b\u4e14\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u5c1d\u8bd5\u8fd9\u4e9b\u9879\u76ee\uff0c\u4e0b\u6b21\u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u9047\u5230\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u65f6\uff0c\u8bf7\u8003\u8651\u4f7f\u7528reducer\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fuseReducer\uff1aReact Hooks\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>react \u4e2d\u7684 usereducer\uff1a\u901a\u8fc7\u4e24\u4e2a\u8ff7\u4f60\u9879\u76ee\u7b80\u5316\u72b6\u6001\u7ba1\u7406 \u4ecb\u7ecd \u72b6\u6001\u7ba1\u7406\u662f\u5728 react \u4e2d\u6784\u5efa\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u90e8\u5206\u3002\u867d\u7136 usestate \u8db3\u4ee5\u7ba1\u7406\u7b80\u5355\u72b6\u6001\uff0c\u4f46\u968f\u7740\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0cusereducer \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u5f3a\u5927\u3001\u53ef\u9884\u6d4b\u7684\u65b9\u6cd5\u6765\u5904\u7406\u5b83\u3002\u53d7 redux \u7684\u51cf\u901f\u5668\u6a21\u5f0f\u7684\u542f\u53d1\uff0cusereducer \u5141\u8bb8\u60a8\u5b9a\u4e49\u72b6\u6001\u8f6c\u6362\u5e94\u5982\u4f55\u53d1\u751f\u4ee5\u54cd\u5e94\u7279\u5b9a\u64cd\u4f5c\uff0c\u4f7f\u5176\u6210\u4e3a\u5177\u6709\u591a\u4e2a\u590d\u6742\u72b6\u6001\u66f4\u65b0\u7684\u573a\u666f\u7684\u7406\u60f3\u9009\u62e9\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\uff1a \u8be6\u7ec6\u89e3\u91ca usereducer\u3001\u5176\u8bed\u6cd5\u4ee5\u53ca\u4f55\u65f6\u4f7f\u7528\u5b83\u3002 \u5b9e\u65bd\u4e24\u4e2a\u5c0f\u9879\u76ee\uff1a \u5177\u6709\u591a\u4e2a\u64cd\u4f5c\u7684\u8ba1\u6570\u5668\uff1a\u8d85\u8d8a\u57fa\u672c\u589e\u91cf\/\u51cf\u91cf\u7684\u793a\u4f8b\uff0c\u5c55\u793a usereducer \u5982\u4f55\u5904\u7406\u591a\u4e2a\u64cd\u4f5c\u7c7b\u578b\u3002 \u5177\u6709\u590d\u6742\u72b6\u6001\u8f6c\u6362\u7684\u5f85\u529e\u4e8b\u9879\u5217\u8868\uff1a\u4e00\u6b3e\u7a81\u51fa usereducer \u7ba1\u7406\u590d\u6742\u72b6\u6001\u5bf9\u8c61\u80fd\u529b\u7684\u5f85\u529e\u4e8b\u9879\u5e94\u7528\u3002 \u8ba9\u6211\u4eec\u6df1\u5165\u4e86\u89e3 usereducer \u5982\u4f55\u7b80\u5316 react \u4e2d\u7684\u72b6\u6001\u7ba1\u7406\uff01 \u7406\u89e3 usereducer \u4ec0\u4e48\u662f usereducer\uff1f usereducer \u662f\u4e00\u4e2a react hook\uff0c\u4e13\u4e3a usestate \u4e0d\u8db3\u7684\u60c5\u51b5\u800c\u8bbe\u8ba1\u3002\u60a8\u4e0d\u662f\u76f4\u63a5\u66f4\u65b0\u72b6\u6001\uff0c\u800c\u662f\u6307\u5b9a\u4e00\u4e2a\u6839\u636e\u5f53\u524d\u72b6\u6001\u548c\u64cd\u4f5c\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u72b6\u6001\u7684\u51cf\u901f\u5668\u51fd\u6570\u3002\u8fd9\u79cd\u58f0\u660e\u5f0f\u65b9\u6cd5\u4f7f\u72b6\u6001\u8f6c\u6362\u4fdd\u6301\u53ef\u9884\u6d4b\uff0c\u5e76\u5141\u8bb8\u60a8\u4ee5\u96c6\u4e2d\u7684\u65b9\u5f0f\u7ba1\u7406\u66f4\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u3002 usereducer \u7684\u8bed\u6cd5 \u4ee5\u4e0b\u662f\u8bed\u6cd5\u7ec6\u5206\uff1a const [state, dispatch] = usereducer(reducer, initialstate); \u767b\u5f55\u540e\u590d\u5236 reducer\uff1a\u5b9a\u4e49\u5982\u4f55\u6839\u636e\u64cd\u4f5c\u66f4\u65b0\u72b6\u6001\u7684\u51fd\u6570\u3002\u5b83\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff1a state\uff1a\u5f53\u524d\u72b6\u6001\u3002 action\uff1a\u5305\u542b\u6709\u5173\u64cd\u4f5c\u4fe1\u606f\u7684\u5bf9\u8c61\uff0c\u901a\u5e38\u5305\u62ec\u7c7b\u578b\u548c\u53ef\u9009\u7684\u6709\u6548\u8d1f\u8f7d\u3002 initialstate\uff1a\u72b6\u6001\u7684\u8d77\u59cb\u503c\u3002 \u793a\u4f8b\uff1a\u4f7f\u7528 usereducer \u7684\u57fa\u672c\u8ba1\u6570\u5668 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3701","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3701"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3701\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}