简体   繁体   English

更新 state 数组后 React Dom 未更新

[英]React Dom not updating after updating a state array

so this function updates this state array: let [Produits, setProduit] = useState(JSON.parse(Devis.produits))所以这个 function 更新了这个 state 数组: let [Produits, setProduit] = useState(JSON.parse(Devis.produits))

 let changeQte = (id, e) => {
    let produittable = Produits;
    produittable.forEach((p) => {
      if (p.id == id) {
        p.quantityAchete = parseInt(e.target.value);
      }
    });
    setProduit(produittable);  
  };

the array did update without any problem but the changes aren't getting re-rendered数组确实更新没有任何问题,但更改没有重新渲染

  {console.log('rendering') ,Produits.map((p) => (
            <div key={p.id} className="product_column flex_center">
              <div className="productItem">{p.nom}</div>
              <div className="productItem">{p.category}</div>
              <div className="productItem">{p.prix_vente} DA</div>
              <input
                onChange={(e) => changeQte(p.id, e)}
                type="number"
                name="qte"
                value={p.quantityAchete}
              />

as you can see i'm loggin to the console to check if that line is getting executed and it does !如您所见,我正在登录控制台以检查该行是否正在执行,并且确实如此! but the values rendered doesn't update !但呈现的值不会更新!

Don't mutate state , do this instead: 不要改变 state ,而是这样做:

 let changeQte = (id, e) => {
    setProduit(existing => existing.map(c => c.id === id ? {...c,quantityAchete: parseInt(e.target.value)} : c))
  };   

These lines:这些行:

    // this line just sets produittable to the same reference as Produits.
    // so now produittable === Produits, it's essentially useless
    let produittable = Produits;
    produittable.forEach((p) => {
      if (p.id == id) {
        // you are mutating 
        p.quantityAchete = parseInt(e.target.value);
      }
    });
    // because produittable === Produits, this doesn't do anything
    setProduit(produittable);  

In addition to what Adam said, besides not modifying the state directly, the reason you're not seeing any changes is because the component only gets rerendered when the state actually changed.除了亚当所说的,除了不直接修改 state 之外,您没有看到任何更改的原因是因为该组件仅在 state 实际更改时才会重新渲染。 And to know whether the state changed, react makes a shallow comparison between the two states.并且要知道 state 是否发生了变化,react 对两种状态进行了浅比较。 Since you modified the state directly, the reference remains the same and as such your component isn't rerendering.由于您直接修改了 state,因此引用保持不变,因此您的组件不会重新渲染。

To expand on Adam's answer, you can also clone the current state Produits and assign it to the local variable produittable and have the state update be recognized.要扩展 Adam 的答案,您还可以克隆当前的 state Produits并将其分配给局部变量produittable并识别 state 更新。

So instead of this:所以代替这个:

let produittable = Produits;

You could simply clone it like so, with the spread operator:您可以像这样使用扩展运算符简单地克隆它:

let produittable = [...Produits];

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

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