简体   繁体   English

如何正确修改json in react?

[英]How to properly modify json in react?

The goal is to execute some crud operations on a json I have stored in state. 目标是在我存储在状态的json上执行一些crud操作。 The json structure is not fixed, but rather changing and dynamic. json结构不是固定的,而是变化和动态的。

I use a recursive functional component to display the json structure. 我使用递归函数组件来显示json结构。 It has a function that records the json hierarchy within which the given value is stored whenever a value is selected. 它有一个记录json层次结构的函数,只要选择了值,就会在其中存储给定值。 Looks something like this: ["arrayName", "arrayIndex", "objectName", "key", "value"]. 看起来像这样:[“arrayName”,“arrayIndex”,“objectName”,“key”,“value”]。 At this point I know where this value resides in the json structure, although I'm not sure how to continue from here. 此时我知道这个值在json结构中的位置,虽然我不知道如何从这里继续。 In the end, I don't find this useful. 最后,我发现这没有用。

The functional component has this signature 功能组件具有此签名

const DisplayValue = ({ structure, category, value, onEdit })

The function looks like this 该功能看起来像这样

const handleEdit = (structure, val) => {
    structure.unshift(val)
    onEdit(structure, category)
  }

And is called depending on some conditions, for example 例如,根据某些条件调用

if (Array.isArray(value)) {
    return (
      <div style={{ border: '1px solid grey', padding: 10 }}>
        {value.map((element, i) => {
          return (
            <div key={i}>
              <DisplayValue
                structure={structure}
                value={element}
                onEdit={() => handleEdit(structure, i)}
              />
            </div>
          )
        })}
      </div>
    )
  }

Finally if the value is neither an array or object: 最后,如果值既不是数组也不是对象:

return (
    <div>
      <Button variant='light' onClick={() => handleEdit(structure, value)}>
        {value}
      </Button>
    </div>
  )

Is there someone with experience with handling and modifying json structures, willing to point me in the right direction? 是否有人有处理和修改json结构的经验,愿意指出我正确的方向?

It looks like you're trying to get a component to directly change the value of one of its props, which is not recommended. 看起来你正试图让一个组件直接改变其中一个道具的值,这是不推荐的。

If your changes only affect downstream/child components then you could store the mutated state in component state instead of component props. 如果您的更改仅影响下游/子组件,那么您可以将变异状态存储在组件状态而不是组件道具中。 If you do this, you'll want to use React Hooks since you're using a functional component. 如果这样做,您将需要使用React Hooks,因为您正在使用功能组件。

If you really do want the props to change, since the changes might affect something higher up in the component hierarchy where the props are provided or in-between, you'll want to either use a callback to affect the value higher up in the component hierarchy or look into using a state management API such as Redux , which is my recommendation for such state modifications in general. 如果你真的想要改变道具,因为这些改变可能会影响提供道具的组件层次结构中的更高层,或者介于其间,你会想要使用回调来影响组件中更高的值。层次结构或使用状态管理API(如Redux) ,这是我对一般状态修改的建议。

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

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