reactjs - React - 意外的 token ,预期的;

标签 reactjs

抛出的错误是:Unexpected token,expected; (9:16) 它指向 renderNumbers() 函数的第一行。我的语法有什么问题吗?我有点困惑,不知道需要更改什么才能使其正常工作。

import React, { PropTypes } from 'react';
import {
  StyleSheet,
  View,
  Text,
  TouchableOpacity
} from 'react-native';

renderNumbers() {
  return this.props.numbers.map(n =>
    <Text>{n}</Text>
  );
}


export default class Counter extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.appName}>
          Countly
        </Text>
        <Text style={styles.tally}>
          Tally: {this.props.count}
        </Text>
        <Text style={styles.tally}>
          Numbers: {this.props.numbers}
        </Text>
        <View>
          {this.renderNumbers()}
        </View>
        <TouchableOpacity onPress={this.props.increment} style={styles.button}>
          <Text style={styles.buttonText}>
            +
          </Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={this.props.decrement} style={styles.button}>
          <Text style={styles.buttonText}>
            -
          </Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={this.props.power} style={styles.button}>
          <Text style={styles.buttonText}>
            pow
          </Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={this.props.zero} style={styles.button}>
          <Text style={styles.buttonText}>
            0
          </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

Counter.propTypes = {
  count: PropTypes.number,
  numbers: PropTypes.func,
  increment: PropTypes.func,
  decrement: PropTypes.func,
  zero: PropTypes.func,
  power: PropTypes.func
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
  },
  appName: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10
  },
  tally: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 20,
    fontSize: 25
  },
  button: {
    backgroundColor: 'blue',
    width: 100,
    marginBottom: 20,
    padding: 20
  },
  buttonText: {
    color: 'white',
    textAlign: 'center',
    fontSize: 20
  }
});

感谢您的帮助。

最佳答案

您不应该使用函数renderNumbers()吗?看起来 renderNumbers 不是 Counter 类的方法,而是代码中的一个单独函数。

顺便说一句,renderNumbers 被定义了两次,尽管它是合法的并且不是问题的原因。

编辑:

如果您想将 renderNumbers() 声明为 prototype methodCounter 的,在类内部定义它:

export default class Counter extends React.Component {

    renderNumbers() {
       ...
    }

    ...

}

否则,使用关键字 function 定义 function :

function renderNumbers() {
    ...
}

这只是 ES6 的语法。

关于reactjs - React - 意外的 token ,预期的;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41815758/

相关文章:

javascript - 你如何在 React 中设置文档标题?

javascript - 相邻的 JSX 元素必须包裹在 React Native 中的封闭体中

reactjs - React + Firebase 托管

javascript - 对来自 Express 的响应使用react JSON.parse 语法错误

javascript - 这个 Reat Todo list 有什么问题吗?

javascript - React 中的确认窗口

node.js - nextjs做一个API请求正常吗?

javascript - React Native Get Async Storage 返回未处理的 Promise

reactjs - redux-form——复选框数组

javascript - Material ui 根据所选主题模式使用调色板原色