尝试将获取的json数据映射到React应用时,“状态”未定义

爱德华·贝拉

我正在从wordpress api获取数据。登录控制台时,我看到了数据数组。在映射数据时,出现错误消息“#myStateName.map不是函数”。

我已经去过ReactJS.org,CSS Tricks甚至Stack Overflow来寻找解决方案,但似乎没有任何效果

class WPHome extends Component {

  constructor(props) {
super(props);
this.state = {
  error: null,
  isLoaded: false,
  details: []
};
  }

  componentDidMount() {
fetch(API_URL)
  .then(res => res.json())
  .then(
    (result) => {
      console.log(result['0'])
      this.setState({
        isLoaded: true,
        details: [result]
      });
    },
    // Note: it's important to handle errors here
    // instead of a catch() block so that we don't swallow
    // exceptions from actual bugs in components.
    (error) => {
      this.setState({
        isLoaded: true,
        error
      });
    }

  )
}

render() {
const { error, isLoaded, details } = this.state;
if (error) {
  return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
  return <div>Loading...</div>;
} else {
  return (
    <div>
      {details.map(item => (
        <div key={item.id}>
          <p>{item.id}</p>
          <div>
            <img src={item._embedded['wp:featuredmedia']['0'].source_url} alt={item.id} />
          </div>
          <p>{item.content}</p>
          <hr />
        </div>
      ))}
    </div>
  );
}
  }

我希望我的数据传递到HTML块中,但是会抛出错误

爱德华·贝拉

我终于弄明白了。虽然必须使用Axios,但它确实有效。

  constructor(props) {
super(props);
this.state = {
  error: null,
  isLoaded: false,
  details: []
};
}

componentDidMount(){
axios
  .get(API_URL)
  .then(response => response.data.map(detail => ({
    image: `${detail._embedded['wp:featuredmedia']['0'].source_url}`,
      content: `${detail.content.rendered}`,
      id: `${detail.id}`
    }))
  )
  .then(details => {
    this.setState({
      details,
      isLoading: false
    });
  })
  .catch(error => this.setState({ error, isLoading: false }));
}


render() {
const { isLoading, details } = this.state;

return (
  <React.Fragment>
      {!isLoading ? (
        details.map(detail => {
          const { id, content, image } = detail;
          return (
            <div key={id}>
              <p>{content}</p>
              <div>
                <img src={image} alt={id} />
              </div>
              <p>{content}</p>
              <hr />
            </div>
          );
        })
      ) : (
          <p>Loading</p>
      )
      }
  </React.Fragment>
)
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

访问组件的道具时,将状态映射到道具后未定义“ this”

将数组映射到对象,键未定义

当我尝试调用时存在未定义的json数据

在React的useEffect()中获取数据将返回“未定义”

在react useState中使用时未定义Firebase数据

在HTML中使用时未定义JSON数据

尝试将结果从 JSON 获取到数组时未定义的索引

状态未定义:从React应用发送发布请求时

React/Redux:获取未定义的初始状态属性

尝试管理从 API 获取的未定义值 - React

尝试从 JSON 数据显示属性时给出未定义

尝试访问json数据(ajax)时未定义

尝试将 json 对象数组存储在状态中,然后将项目映射到渲染中

尝试对IMGUR api进行Ajax调用时,数据是否未定义?

尝试从 JSON 响应中获取数据时,获取“变量未定义”或“variable.map”不是函数

我正在尝试从烧瓶应用程序获取数据,但出现此错误:“未捕获的ReferenceError:_data未定义”

React应用:获取后未定义的值

使用 React 在 app.js 中获取错误“返回未定义”(“数据未定义”)

使用 React 钩子,为什么获取的数据未定义?

localStorage获取未定义状态

尝试获取令牌返回未定义

在 NodeJS 中从 JSON 中获取数据导致未定义

无法从“未定义”的 JSON 中获取数据

尝试映射 React 中的对象数组,返回未定义

尝试使用数组映射函数时未定义 React 属性

尝试在React中映射子对象时发生未定义的错误

将 onChange 事件从映射的子级传递到父级的状态返回“未定义”错误

React:如何使用传单通过获取调用将geojson映射到状态?

状态在React中保持未定义