简体   繁体   English

React:如何处理每次发生 state 更改时重新渲染的 Table 中的大 td Input 元素?

[英]React: How to deal with large td Input element in Table that re-render everytime when state changed occurs?

I'm not sure if this is the right way to push table element in React, but I've been doing this all the time.我不确定这是否是在 React 中推送表格元素的正确方法,但我一直在这样做。 So this for loop below will get executed everytime re-render occurs or in this case when I change my input.因此,每次重新渲染发生时,或者在这种情况下,当我更改输入时,都会执行下面的这个 for 循环。 what if I have like 100 Input?如果我有 100 个输入怎么办?

Questions:问题:

  1. Is this the right way to draw table element?这是绘制表格元素的正确方法吗?
  2. wouldn't re-render cause bad performance especially if you have some kind of loop before return?重新渲染不会导致性能不佳,特别是如果您在返回之前有某种循环?
  3. If this is not the right way to do it.如果这不是正确的方法。 Please show me the right way to do it.请告诉我正确的方法。
function App() {
const [inputs,setInputs] = useState({input1:'',input2:'',input3:''})


function handleOnChange(e){
  const {name,value} = e.target

  setInputs(prev => ({...prev, [name]:value}))
}

let tableElement = []

for(let i = 1; i <= 3; i++){
  tableElement.push(<tr  key={i}>
    <td><Input value={inputs[`${i}`]} name={`input${i}`} onChange={handleOnChange} /></td>
  </tr>)
}

  return (
      <div>
        <Table>
          <tbody>
             {tableElement}
          </tbody>
        </Table>
      </div>

  );
}

export default App;

Given code:给定代码:

let tableElement = []

for (let i = 1; i <= 3; i++){
  tableElement.push((
    <tr key={i}>
      <td>
        <Input value={inputs[`${i}`]} name={`input${i}`} onChange={handleOnChange} />
      </td>
    </tr>
  ))
}

return (
  <div>
    <Table>
      <tbody>
         {tableElement}
      </tbody>
    </Table>
  </div>
);

Questions:问题:

  1. Is this the right way to draw table element?这是绘制表格元素的正确方法吗?

It isn't wrong, though it may not be the most common way to render out an array to JSX.这没有错,尽管它可能不是将数组渲染到 JSX 的最常见方式。 Typically you may opt to map an array to JSX通常你可以选择 map 一个数组到 JSX

const tableElement = [...Array(3).keys()].map((i) => (
  <tr key={i}>
    <td>
      <Input value={inputs[`${i}`]} name={`input${i}`} onChange={handleOnChange} />
    </td>
  </tr>
));

return (
  <div>
    <Table>
      <tbody>
         {tableElement}
      </tbody>
    </Table>
  </div>
);

Or directly in the JSX或者直接在 JSX

return (
  <div>
    <Table>
      <tbody>
        {[...Array(3).keys()].map((i) => (
          <tr key={i}>
            <td>
              <Input
                value={inputs[`${i}`]}
                name={`input${i}`}
                onChange={handleOnChange}
              />
            </td>
          </tr>
        ))}
      </tbody>
    </Table>
  </div>
);
  1. wouldn't re-render cause bad performance especially if you have some kind of loop before return?重新渲染不会导致性能不佳,特别是如果您在返回之前有某种循环?

I suppose there is a potential for poor performance, but due to the nature of the UI needing to map out a bunch of array elements when the component renders this is work that would be necessary anyway.我想可能会出现性能不佳的情况,但是由于 UI 的性质,当组件渲染时需要 map 输出一堆数组元素,无论如何这都是必要的工作。

React is already pretty optimized out-of-the-box. React 已经非常优化,开箱即用。 It uses a reconciliation process to determine what mapped elements actually changed from the previous render and only rerenders what is necessary.它使用协调过程来确定哪些映射元素与之前的渲染相比实际上发生了变化,并且只重新渲染必要的部分。 The React key is used when mapping arrays to help in this regard.在映射 arrays 时使用 React 键来提供帮助。

  1. If this is not the right way to do it.如果这不是正确的方法。 Please show me the right way to do it.请告诉我正确的方法。

I shared the more common methods of mapping an array to JSX in point #1 above.我在上面的第 1 点中分享了将数组映射到 JSX 的更常见的方法。

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

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