【问题标题】:Access json nested with react使用 React 访问嵌套的 json
【发布时间】:2018-06-19 03:37:43
【问题描述】:

你好吗?我在来自 Mongo 的 Json 中创建了更多级别,但是,当访问具有两个以上嵌套级别的元素时,他们无法读取未定义的属性。

在一个 Json 中可以访问多少数据有限制吗?当在 render() 之外通过 console.log 访问时,相同,但在 render 方法内,甚至会进入两个级别。

感谢您的帮助。

【问题讨论】:

  • 您能否分享任何代码、示例输入、异常或预期输出。
  • 不,JSON 字符串中表示的数据的“深度”没有限制。也许您正在处理异步并且不知道它
  • 使用redux-saga搜索api

标签: javascript json reactjs


【解决方案1】:

我使用redux-saga搜索api:

import { all, takeEvery, put, call } from 'redux-saga/effects';
import actions from './actions';

const api = 'http://localhost:3003/api/estado/infraestrutura/habitacao';

function* getData() {
  try {
    const data = yield call (
      async () =>
        await fetch(api)
          .then(res => res.json())
          .then(res => res)
          .catch(error => error)
    )

    yield put({
      type: actions.INPUT_DATA,
      data
    })
  } 
  catch (error) {
    console.log(error)
  }
}

export default function* rootSaga() {
  yield all([takeEvery(actions.GET_DATA, getData)]);
}

【讨论】:

    猜你喜欢
    • 2018-03-13
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2013-11-18
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多