反应-错误结果图不是函数吗?

亚什·马马特(Yash Marmat)
  • 框架:React
  • 错误类型:result.map不是函数

我正在阅读《学习React之书》一书,我尝试了使用骇客新闻API的以下代码,该代码效果很好,但不适用于该API。我不知道为什么会出现此错误,请帮忙。

链接到我的沙箱-> https://codesandbox.io/s/react-setup-forked-q0hti?file=/src/App.js

import React, { Component } from "react";
    
// API chunks
const PATH_BASE = "https://jsonplaceholder.typicode.com/todos";
const PATH_SEARCH = "/search";
const PARAM_SEARCH = "query=";
const DEFAULT_QUERY = "redux";
    
//const url = `${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${DEFAULT_QUERY}`;
//console.log(url);
    
class App extends Component {
  constructor() {
    super();
    this.state = {
      result: null
    };
    this.hitStories = this.hitStories.bind(this);
  }
   
  // handling the local state value
  hitStories(result) {
    this.setState({
      result
    });
  }
    
  // lifecycle method
  // Note: componenetDidMount runs after the render method
  componentDidMount() {
    fetch(`${PATH_BASE}${PATH_SEARCH}?${PARAM_SEARCH}${DEFAULT_QUERY}`)
      .then((response) => response.json())
      .then((json_result) => this.hitStories(json_result))
      .catch((error) => error);
  }

  render() {
    console.log(this.state)
    const { result } = this.state;

    if (!result) {
      return null;
    }
    
    return (
      <div>
        <h2>Fetch API in React</h2>
        {result.map((
          item 
        ) => (
         <div>
           {item.title}
          </div>
        ))}
      </div>
    );
  }
}
    
export default App;
臭氧

.map用于数组,您result不是数组而是对象。尝试这个:

render() {
  const { result } = this.state;

  return result ? (
    <div>
      <h2>Fetch API in React</h2>
      { Object.values(result).map((item) => (
        <div>{item.title}</div>
      ))
      }
    </div>
  ): null;
}

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

.map 不是函数 - 反应

来自分类Dev

反应Redux错误TypeError:sourceSelector不是函数

来自分类Dev

反应错误:TypeError:render不是函数

来自分类Dev

反应TypeError:x不是函数

来自分类Dev

反应:TypeError:调度不是函数

来自分类Dev

反应:“.map 不是函数”问题

来自分类Dev

地图不是反应中的函数

来自分类Dev

'this.props.tasks.map 不是函数'错误-反应错误

来自分类Dev

未捕获的TypeError:r不是函数-Google登录错误-反应

来自分类Dev

反应handleChange抛出错误“ this.setState不是函数”

来自分类Dev

反应错误更改 this.setState 不是一个函数

来自分类Dev

类型错误:this.state.user.map 不是反应中的函数

来自分类Dev

反应类型错误:this.state.entries.map 不是函数

来自分类Dev

反应本机漏斗图

来自分类Dev

反应本机错误,需要未知的模块“反应”

来自分类Dev

Flex box 不是预期的结果反应原生?

来自分类Dev

反应-传递(this)onClick吗?

来自分类Dev

反应挂钩函数依赖

来自分类Dev

映射函数反应

来自分类Dev

反应错误消息丢失

来自分类Dev

R闪亮:反应错误

来自分类Dev

ESLint反应解析错误

来自分类Dev

反应UseState正确/错误

来自分类Dev

反应错误消息丢失

来自分类Dev

gulp错误与反应

来自分类Dev

反应内联样式错误

来自分类Dev

JQuery Post 错误反应

来自分类Dev

反应本机转换错误

来自分类Dev

反应不变错误