简体   繁体   English

React array.map 返回预期的赋值或函数调用,而是看到了一个表达式

[英]React array.map returns Expected an assignment or function call and instead saw an expression

I have written a very simple react component where-in I try to fetch an API from the jsonplaceholder/typicode website.我编写了一个非常简单的反应组件,其中我尝试从 jsonplaceholder/typicode 网站获取 API。 I am able to successfully get the result and set it in state.我能够成功获得结果并将其设置为状态。 Now, when I try to print those result on the screen using array.map, I get the following error现在,当我尝试使用 array.map 在屏幕上打印这些结果时,出现以下错误

Expected an assignment or function call and instead saw an expression  no-unused-expressions

This is how the component looks like:这是组件的样子:

class App extends Component {
  constructor(props) {
    super(props);
    this.state = { 
      result: []
     }
  }

  componentDidMount() {
    fetch("https://jsonplaceholder.typicode.com/posts")
    .then(res=> res.json())
    .then(result=>{
      this.setState({
        result
      })
    })
  }
  
  render() { 
    return ( 
      <div>
        {this.state.result.map(post=>{
          <h1>{post.title}</h1>
        })}
      </div>
     );
  }
}

I don't understand what am I doing wrong here.我不明白我在这里做错了什么。 Someone please have a look有人请看

You forgot to return你忘了return

<div>
  {this.state.result.map((post) => {
    return <h1>{post.title}</h1>
  })}
</div>

Codesandbox for demo演示代码沙盒

编辑深情borg-hugm9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React 期望一个赋值或函数调用,而是看到一个表达式 - React Expected an assignment or function call and instead saw an expression 期望一个赋值或函数调用,而是看到一个表达式反应路由器 - Expected an assignment or function call and instead saw an expression react router 反应错误 [期望赋值或函数调用,而是看到一个表达式] - React error [Expected an assignment or function call and instead saw an expression] 期望一个赋值或函数调用,而是看到一个表达式做出反应 - expected an assignment or function call and instead saw an expression react 期望分配或 function 调用,而是看到表达式 React JS - Expected an assignment or function call and instead saw an expression React JS React:需要一个赋值或函数调用,而是看到一个表达式 - React : Expected an assignment or function call and instead saw an expression React-期望赋值或函数调用,而是看到表达式无法编译 - React - Expected an assignment or function call and instead saw an expression failed to compile 需要一个赋值或函数调用,而是在React中看到一个表达式 - Expected an assignment or function call and instead saw an expression in React 期望分配或 function 调用,而是看到一个表达式 - React - Expected an assignment or function call and instead saw an expression - React 反应问题 - 预期分配或 function 调用,而是看到一个表达式 - React issue - Expected an assignment or function call and instead saw an expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM