首页 > 解决方案 > 获取 JSON API 中的所有数组

问题描述

我正在使用 Promise 样式从 API 获取 JSON 数组。但是,我想获取 JSON URL 中的所有数组。是否有使用 useEffects for React 的简单解决方案?此外,我是否可以在 useEffect 中动态搜索 JSON 数组,是否应该恢复使用 redux 并从用户查询中获取快照。

JSON 数组示例:

"shirt": [
 {
   "shirt": "red
   "price": 3
 }
 {
  "shirt": "red
  "price": 3
 }
]
"hat": [ 
 {
  "shirt": "red
  "price": 4
 }
] .....

json 数组

我目前的解决方案:

  const [data, setData] = useState([]);

  useEffect(() => {
    fetch(productURL)
      .then(response => response.json())
      // .then(data => data.json())
      .then(json => setData(json.sneakers))
      .catch(error => alert(error))
      .finally(setLoading(false));
  }, []);```

标签: javascriptjsonreactjsreact-nativeapi

解决方案


推荐阅读