简体   繁体   English

母组件控制子组件的输入,但不会在更改时重新呈现

[英]Mother component controlling input for child components, but doesn't rerender on change

I have a React component, where i have a category array, that i map, and index the data form another object.我有一个 React 组件,其中我有一个类别数组,即 map,并从另一个 object 索引数据。


  const labelComponents = categories.map((category, index) =>{
    const data = globalState.filter(systemLabel => systemLabel.value === category.categoryKey).pop()
    return(
      <Label
      key={data && data.text ? data.text + category : category + index}
      category={category} 
      data={globalState.filter(systemLabel => systemLabel.value === category.categoryKey).pop()}
      deleteLabel={deleteLabel}
      updateLabelValue={updateLabelValue}
      />
    )
  })

I pass in the function updateLabelValue where i try to update the specefic text attribute on the chosen object.我传入了 function updateLabelValue ,我尝试更新所选 object 上的特定text属性。

This function could probably be refactored, but it works for now.这个 function 可能会被重构,但它现在可以工作。

 const updateLabelValue = (categoryKey, value) =>{
    const labelToUpdate = globalState.filter(entry => entry.value === categoryKey).pop();
    const index = globalState.indexOf(labelToUpdate);
    labelToUpdate.text = value;
    globalState[index] = labelToUpdate
    console.log(globalState)
    setGlobalState(globalState)
  }

I sat my key in euqal to the data.text attribute, so it would update automatically, but that does not happen我将 euqal 中的密钥放在data.text属性上,所以它会自动更新,但这不会发生

The issue here of course, is that i map my categories , but access my globalState object, so therefore it does not automatically update.当然,这里的问题是我 map 我的categories ,但访问我的globalState object,因此它不会自动更新。

You are mutating React state (and React doesn't like this at all).你正在改变 React state (而 React 根本不喜欢这个)。 That can trigger weird problems and make things doesn't re-render as expected.这可能会引发奇怪的问题,并使事情不会按预期重新渲染。

  • const labelToUpdate = globalState.filter(entry => entry.value === categoryKey).pop(); const labelToUpdate = globalState.filter(entry => entry.value === categoryKey).pop(); Pop is a mutable method although I don't know if it's a problem in this case, as filter is purely functional. Pop 是一种可变方法,尽管我不知道在这种情况下是否有问题,因为 filter 纯粹是功能性的。 Anyway you can use find instead of filter ( const labelToUpdate = globalState.find(entry => entry.value === categoryKey) ) if you only want one element or slice(-1)[0] after filter if there's several elements and you only want the last one ( const labelToUpdate = globalState.filter(entry => entry.value === categoryKey).slice(-1)[0] )无论如何,如果您只想要一个元素或在过滤器后需要一个切片(-1)[0](如果有多个元素),则可以使用 find 而不是过滤器( const labelToUpdate = globalState.find(entry => entry.value === categoryKey) )并且你只想要最后一个( const labelToUpdate = globalState.filter(entry => entry.value === categoryKey).slice(-1)[0]

  • The function updateLabelValue mutates globalState. function updateLabelValue 改变 globalState。 In fact you have already changed the state in globalState[index] = labelToUpdate when you invoke setState.事实上,当您调用 setState 时,您已经在globalState[index] = labelToUpdate中更改了 state。

To fix this you can pass the index of the element to the function and make something like this要解决此问题,您可以将元素的索引传递给 function 并进行类似的操作

 const updateLabelValue = (value, index) =>{
    const newState = globalState.map((item, i) => {
      if(index === i){ return value }
      else{ return item }
    }
    setGlobalState(newState)
  }

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

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