簡體   English   中英

渲染的反應格式-語法-意外標記

[英]React formatting for render — syntax — unexpected token

我想知道為什么我的 React class 的格式是錯誤的? 它說應該有一個括號,但我確實包括了一個括號? 不過,我可能忘記了語法格式的某些方面:(這是我的 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) 

(在渲染())

首先,您必須返回單個元素。 相反,您返回 2。然后,渲染方法實現中的最后一行是無法訪問的(在返回之后)。 嘗試這個:

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')
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM