Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 通过保存在会话存储中的对象进行解析_Reactjs_Ecmascript 6_React Props - Fatal编程技术网

Reactjs 通过保存在会话存储中的对象进行解析

Reactjs 通过保存在会话存储中的对象进行解析,reactjs,ecmascript-6,react-props,Reactjs,Ecmascript 6,React Props,因此,我分配了一个具有两个属性的对象,第一个属性“items”是保存在会话存储中的对象数组 [{"id": 0, "name": "example"}] 第二个只是一个字符串 "this is an example" 当I console.log postingItems正常工作时,我发现该对象已合并 method = (e) => { const postingItems = Object.assign({}, { items: sessionSt

因此,我分配了一个具有两个属性的对象,第一个属性“items”是保存在会话存储中的对象数组

[{"id": 0, "name": "example"}]
第二个只是一个字符串

"this is an example"
当I console.log postingItems正常工作时,我发现该对象已合并

method = (e) => {
        const postingItems = Object.assign({}, {
          items: sessionStorage.getItem("items1"),
          method: sessionStorage.getItem("method")
        });
        sessionStorage.setItem("items", postingItems)
        console.log(postingItems)
  }
但是当我在会话存储中保存postingItems,然后在控制台[Object]上显示console.log时

sessionStorage.setItem("finalItems", postingItems);
console.log(sessionStorage.getItem(finalItems));

您需要首先对对象进行字符串化,然后在加载/获取时进行解析

sessionStorage.setItem("finalItems", JSON.stringify(postingItems));
console.log(JSON.parse(sessionStorage.getItem("finalItems")));

您需要首先对对象进行字符串化,然后在加载/获取时进行解析

sessionStorage.setItem("finalItems", JSON.stringify(postingItems));
console.log(JSON.parse(sessionStorage.getItem("finalItems")));