【问题标题】:Im trying to create a button for my react native assignment but i keep getting an unexpected token, syntax error我试图为我的反应原生分配创建一个按钮,但我不断收到一个意外的标记,语法错误
【发布时间】:2020-06-05 17:33:16
【问题描述】:

我是 react-native 的新手,我正在尝试为我的 react-native 分配创建一个按钮,但我不断收到意外的标记,语法错误。这是下面的代码(他们说错误在第 6 - 11 行)

我正在使用https://snack.expo.io/ 来测试我的代码

以下是它对以下错误所说的确切内容:

''' /module:/App.js: 意外令牌 (8:18)

6 - const Todo = props => (

7 - 查看>

8 - 标题="删除" />

9 - {props.todo.text}

10 - /查看>

11-)

评估模块://App.js.js

加载模块://App.js '''

由于某种原因,堆栈溢出它没有显示(视图、按钮、文本、滚动视图),但它们在那里,但您可以根据需要添加它们以使代码正常工作。

我查看了代码,但似乎无法得到我所缺少的,我将在此添加整个代码,以便您可以看到我正在使用的所有代码。

感谢您的帮助!

import React from 'react'; 
import {View, Button, Text, ScrollView} from 'react-native'

let id = 0

const Todo = props => (
<View>
  <Button><onPress={props.onDelete}> title="delete" />
  <Text>{props.todo.text}</Text>
</View>
)

export default class App extends React.Component {
  constructor(){
    super()
    this.state = {
      todos: [],
    }
  }
}

addTodo() {
  id++
  const text = 'TODO number ${id}'
  this.setState({
    todos: [
      ...this.state.todos,
{id: id, text: text, checked: false},
    ],
  })
}

removeTodo(id) {
  this.setState({
    todo: this.state.todo.filter(todo => todo.id !
    )
  })
}

toggleTodo(id) {
  this.setState({
    todos: this.site.todo.map(todo => {
if (todo.id !== id) return todo 
return {
  id: todo.id
  text: todo.text
  checked: !todo.checked
      }
    })
  })
}

render() {
  return(
    <View>
      <Text>Todo count: this.state.todo.length}
        </Text>
        <Text>Unchecked todo count: this.state.todos.filter(todo => !todo.checked).length}    </Text>
        <Button onPress={() => this.addToDo()} title ="Add TODO" />
        <ScrollView>
        {this.state.todo.map(todo => (
          <Todo
          onToggle={() => this.toggleTodo(todo.id)}
          onDelete={() => this.removeTodo(todo.id)}
          todo={todo}
          />
        ))}
        </ScrollView>
      </View>
  )
 }
}

【问题讨论】:

    标签: react-native


    【解决方案1】:

    请更改待办事项功能如下

     const Todo = props => (
        <View>
         <Button onPress={props.onDelete} title="delete" />
         <Text>{props.todo.text}</Text>
        </View>
     )
    

    但即使在此之后,您的代码也不会编译。您的代码中有很多编译问题。请检查并重试。

    【讨论】:

      【解决方案2】:

      但我已经在代码的顶部找到了这些

      import React from 'react'; 
      import {View, Button, Text, ScrollView} from 'react-native'
      

      除非那不是导入按钮的正确方法?所以我肯定错过了一些东西。

      那我试试

      import Button from 'react-native';
      

      看看会发生什么

      感谢您的帮助

      【讨论】:

        猜你喜欢
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        • 2021-11-28
        • 2022-08-12
        • 2019-05-09
        • 2021-03-03
        相关资源
        最近更新 更多