1

我是 ReactJS 的新手,我想从 Traversy Media 的视频教程中学习基础知识,我将代码应用到他必须做的事情上。但我被困在这部分,因为我得到一个错误,无法编译,如下所示: 编译失败

这是我写的代码:

应用程序.js

import React, { Component } from 'react';
import Todos from './components/Todos';
import './App.css';

class App extends Component {
  state = {
   todos: [
     {
       id: 1,
       title: 'Take out he trash',
       completed: false
     },
     {
       id: 2,
       title: 'Have a Dinner',
       completed: false
     },
     {
       id: 3,
       title: 'Meeting with Boss',
       completed: false
     },
   ]
  };

  render() {
    return (
      <div className="App">
        <Todos todos={this.state.todos} />
      </div>
    );
  }
}

export default App;

Todos.js

import React, {Component} from 'react';
import TodoItem           from './TodoItem';

class Todos extends Component {
  render() {
    return this.props.todos.map((todo) => (
      <TodoItem/>
    ));
  }
}

export default Todos;

TodoItem.js

import React, { Component } from 'react';

export class TodoItem extends Component{
  render(){
    return{
      <div>
        <p>Hello</p>
      </div>
    }
  }
}

export default TodoItem;
4

3 回答 3

3

它应该是这样的——

import React, { Component } from 'react';

export class TodoItem extends Component{
  render(){
    return(
      <div>
        <p>Hello</p>
      </div>
    )
  }
}

export default TodoItem;

于 2019-01-23T06:23:09.257 回答
2

它应该是

return(
  <div>
    <p>Hello</p>
  </div>
)

为方法或变量使用大括号

于 2019-01-23T05:32:18.413 回答
1

TodoItem.js 返回语句中存在语法错误。只需替换此代码

    import React, { Component } from 'react';

     export class TodoItem extends Component{
       render(){
         return(
           <div>
            <p>Hello</p>
           </div>
               )
             }
           }

      export default TodoItem;
于 2019-01-23T05:35:13.617 回答