gpt4 book ai didi

javascript - 如何在使用 useReducer 时在 react js 中进行优化,循环传递给子组件的状态

转载 作者:行者123 更新时间:2023-12-04 08:07:43 26 4
gpt4 key购买 nike

我正在尝试创建待办事项列表。

function App() {
return (
<div className="App">
<UseReducerToDo/>
</div>
);
}

const { useReducer, useState } = React;

const reducer = (state, action) => {
return [...state, { id: Date.now(), ToDo: action, completed: false }];
};

function UseReducerToDo() {
const [state, dispatch] = useReducer(reducer, []);
const [Todo, setTodo] = useState("");

const addToTODO = (e) => {
e.preventDefault();
dispatch(Todo);
};

return (
<div>
<form onSubmit={addToTODO}>
<input
type="text"
value={Todo}
onChange={(e) => setTodo(e.target.value)}
></input>
</form>
{state.length > 0
? state.map((todo) => <DispToDo key={todo.id} todo={todo} />)
: "no pending items"}
</div>
);
}

function DispToDo({ todo }) {
console.log("DispToDo", todo);
return (
<div>
<p>
{todo.ToDo + "->"} status {todo.completed ? "done" : "pending"}
</p>
</div>
);
}

ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>

现在,代码编译并运行良好。问题在于性能。
当应用程序运行时
initial screen
添加并提交第一个元素时
after first element added
现在出现了实际问题
after each keypress
我发现文本框中的每个按键都会刷新子组件 DispToDo .这很明显,因为控制台中的日志为每次按键加起来。 state没有变化但在 DispToDo 中有刷新.
有没有办法优化这个?

最佳答案

您可以记住您的 state.map() useMemo() 钩:

const todos = useMemo(() => (
state.map((todo) => <DispToDo key={todo.id} todo={todo} />)
), [state]);
请参阅下面的完整演示:

function App() {
return (
<div className="App">
<UseReducerToDo/>
</div>
);
}

const { useMemo, useReducer, useState } = React;

const reducer = (state, action) => {
return [...state, { id: Date.now(), ToDo: action, completed: false }];
};

function UseReducerToDo() {
const [state, dispatch] = useReducer(reducer, []);
const [Todo, setTodo] = useState("");

const addToTODO = (e) => {
e.preventDefault();
dispatch(Todo);
};

const todos = useMemo(() => (
state.map((todo) => <DispToDo key={todo.id} todo={todo} />)
), [state]);

return (
<div>
<form onSubmit={addToTODO}>
<input
type="text"
value={Todo}
onChange={(e) => setTodo(e.target.value)}
></input>
</form>
{state.length > 0 ? todos : "no pending items"}
</div>
);
}

function DispToDo({ todo }) {
console.log("DispToDo", todo);
return (
<div>
<p>
{todo.ToDo + "->"} status {todo.completed ? "done" : "pending"}
</p>
</div>
);
}

ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>

关于javascript - 如何在使用 useReducer 时在 react js 中进行优化,循环传递给子组件的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66141214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com