将json响应保存为常量

时间:2017-02-16 04:52:27

标签: javascript json reactjs

我正在使用ReactJS,我正在尝试将json数组保存为'const'。像这样:

fetch(url)
  .then(response => response.json())
  .then(json => {
    this.setState({
      data: json
    });  
  });

但我不想将其保存在状态中,因为数组不会改变。

有什么想法吗?

谢谢:)

2 个答案:

答案 0 :(得分:3)

将其保存在变量或localstorage中

let dataJson = {};
fetch(url)
  .then(response => response.json())
  .then(json => {
    dataJson = json
  });

答案 1 :(得分:2)

let data;

fetch(url)
  .then(response => response.json())
  .then(json => {
     data = json
  });