Skip to content

How to update the specific element in JSON data with setState in React  #2717

@spvjebaraj

Description

@spvjebaraj

Hi,

I am working in React with TypeScript. I am using the below JSON format to build the tree structure to display the content as folder. I stored the below JSON data in state object as treeViewData.

{
  "path": "Parent Site",
  "name": "Parent Site",
  "type": "directory",
  "isRoot": true,
  "children": [
    {
      "path": "Parent Site/Sub Site 1",
      "name": "Sub Site 1",
      "type": "directory",
      "children": [
        {
          "path": "Parent Site/Sub Site 1/Documents",
          "name": "Documents",
          "type": "directory",
          "children": []
        },
        {
          "path": "Parent Site/Sub Site 1/My Documents",
          "name": "Documents",
          "type": "directory",
          "children": [
            {
              "path": "Parent Site/Sub Site 1/My Documents/Folder1",
              "name": "Folder1",
              "type": "directory”,
"children": []
            },
            {
              "path": "Parent Site/Sub Site 1/My Documents/Folder2",
              "name": "Folder2",
              "type": "directory”,
"children": []
            }
          ]
        },
        {
          "path": "Parent Site/Sub Site 1/My New Documents",
          "name": "Documents",
          "type": "directory",
          "children": []
        }
      ]
    },
    {
      "path": "Parent Site/Sub Site 2",
      "name": "Sub Site 2",
      "type": "directory",
      "children": [
        {
          "path": "Parent Site/Sub Site 2/Documents",
          "name": "Documents",
          "type": "directory",
          "children": [
            {
              "path": "Parent Site/Sub Site 2/Documents/Folder1",
              "name": "Folder1",
              "type": "directory”,
"children": []
            },
            {
              "path": "Parent Site/Sub Site 2/Documents/Folder2",
              "name": "Folder2",
              "type": "directory”,
"children": []
            },
            {
              "path": "Parent Site/Sub Site 2/Documents/Folder3",
              "name": "Folder3",
              "type": "directory",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

From the above JSON data, I want to insert new set of properties in the Children object. Let us consider the below child which is resided in the Parent Site/Sub Site 2.

"path": "Parent Site/Sub Site 2/Documents/Folder1",

In this current node, Children array is empty. I want to insert new set of four properties to be added like below.

 "path": "Parent Site/Sub Site 2/Documents/Folder1/Inside New Folder",
 "name": "Inside New Folder",
"type": "directory”,
"children":[]

How to do this in React? How to perform the setState operation of this scenario? Please help me.

Thanks
Philip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions