React格式化的渲染--语法--意外的标记

问题描述 投票:0回答:1

我想知道为什么我的React类的格式是错误的?它说theres应该是一个paranthesis,但我确实包含了一个paranthesis?我可能忘记了语法格式的某些方面 :( 这是我的js文件的一个片段。

function App() {
  state = {
    posts = []; 
    input = '';
  };
  componentDidMount = () => {
    this.getPosts();
  };
  getPosts = () => {
    axios.get('(server)')
    // replace (server) with server link
      .then((response) => {
        const data = response.data;
        this.setState({ posts: data });
      })
  }
  displayBlogPost = (posts) => {
    if (!posts.length) return null;
    return posts.map((post, index) => (
      <div key={index} className="post__display">
        <h3>"name " + post.name </h3>
        <h3>"City " + post.location </h3>
        <h3>"Requesting " + post.type </h3>
        <h3> post.message </h3> 
      </div>
    ));
  };

  render() {
      return (
        <div className="App">
        <header> 
            <h2> Covunity </h2> 
            <SearchExample items= { this.state.posts } />
        </header>
            </div>
        <div className="gallery"> 
        {this.displayBlogPost(this.state.posts)} </div> 
    )
    document.getElementById('root')
  }
}

而错误信息是

Syntax error: Unexpected token, expected ";" (54:12) 

(在render())

reactjs error-handling syntax frontend
1个回答
0
投票

首先,你必须返回一个单一元素。而不是返回2个。然后,render方法实现中的最后一行是无法到达的(在返回之后)。试试这个。

render() {
  return (
    <div>
      <div className="App">
        <header>
          <h2> Covunity </h2>
          <SearchExample items={this.state.posts}/>
        </header>
      </div>
      <div className="gallery">
        {this.displayBlogPost(this.state.posts)}
      </div>
    </div>
  )
  // document.getElementById('root')
}
© www.soinside.com 2019 - 2024. All rights reserved.