简体   繁体   English

如何在不使用 state 变量的情况下实现实时重新渲染?

[英]How do I achieve live re-render without using state variables?

Consider the following array of objects:考虑以下对象数组:

const tasks = [
    {
      text: "Finish this page",
      deadline: new Date("05/01/2022"),
      status: "complete",
      // ToDo: Add priority fields
    },
    {
      text: "Finish Pierian UI/UX",
      deadline: new Date("05/10/2022"),
      status: "incomplete",
    },
    {
      text: "Finish Internship",
      deadline: new Date("05/05/2022"),
      status: "incomplete",
    },
    {
      text: "Anaadyanta",
      deadline: new Date("05/12/2022"),
      status: "incomplete",
    },
    {
      text: "Random task",
      deadline: new Date("05/19/2022"),
      status: "incomplete",
    },
  ];

Consider the following function:考虑以下 function:

{tasks.map((element) => {
        return (
          <p
            key={element.name}
            className={element.status === "complete" && "strike"}
            style={{ borderLeft: "2px solid red" }}>
            <Checkbox
              size="small"
              onClick={() => {
                if (element.status === "incomplete")
                  element.status = "complete";
                else if (element.status === "complete") {
                  element.status = "incomplete";
                }
                // ToDo : Add few things like popping
              }}
              color="success"
              checked={element.status === "complete" && true}
            />
            {element.text}
          </p>
        );
      })}

Now since element.status is not a state, changing its value doesnt re render the JSX element.现在因为element.status不是 state,改变它的值不会重新渲染 JSX 元素。 How do I achieve this?我如何实现这一目标? I thought of using a state variable, but that when changed would reflect changes in all rendered elements and not only the current element.我想过使用 state 变量,但是当更改时会反映所有渲染元素的变化,而不仅仅是当前元素。

You can't - at least, not in any good way.你不能——至少,不能以任何好的方式。 The way React determines when it needs to re-render is when a state setter is called. React 确定何时需要重新渲染方式是在调用 state setter 时。

A React application should, whenever possible, have the appearance of the application come as much from state as possible. React 应用程序应该尽可能让应用程序的外观尽可能来自 state。 That is, ideally, given the state of all components, you should be able to determine everything about what's being rendered to the user.也就是说,理想情况下,给定所有组件的 state,您应该能够确定有关向用户呈现的内容的所有内容。

I thought of using a state variable, but that when changed would reflect changes in all rendered elements and not only the current element.我想过使用 state 变量,但是当更改时会反映所有渲染元素的变化,而不仅仅是当前元素。

Why would it necessarily change all elements?为什么它一定会改变所有元素? Just change the one element being iterated over.只需更改被迭代的一个元素。 To make things easier, use complete: boolean instead of status: 'complete' | 'incomplete'为了让事情更简单,使用complete: boolean而不是status: 'complete' | 'incomplete' status: 'complete' | 'incomplete' . status: 'complete' | 'incomplete'

const [tasks, setTasks] = useState([
    {
      text: "Finish this page",
      deadline: new Date("05/01/2022"),
      complete: true
      // ToDo: Add priority fields
    },
    // ...
]);
// ...
{tasks.map((element, i) => {
    ...
    onClick={() => {
        setTasks(tasks.map(
            (task, j) => i === j ? { ...task, complete: !element.complete } : task
        );
    }} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM