React - 预期的意外令牌;

时间:2017-01-23 21:16:03

标签: reactjs

抛出的错误是:意外的令牌,预期; (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
  }
});

感谢您的帮助。

2 个答案:

答案 0 :(得分:6)

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

顺便说一下,renderNumbers被定义了两次,虽然它是合法的而不是问题的原因。

编辑:

如果您要将renderNumbers()声明为类Counter的{​​{3}},请在类中定义它:

export default class Counter extends React.Component {

    renderNumbers() {
       ...
    }

    ...

}

否则,请使用关键字function来定义prototype method

function renderNumbers() {
    ...
}

它只是ES6的语法。

答案 1 :(得分:1)

您的组件遇到此错误的原因是由于以下原因:  1.如果在ES6类之外定义函数,则必须使用function关键字。但是,如果执行此操作,则在调用该函数时,this引用将不确定。  2.如果在React Component(它只是一个ES6类)中定义一个函数,则在函数定义前面不需要“function”关键字。

选项1:

function renderNumbers() {
    return <Text>...</Text>
}

class Counter extends React.component {
  render() {
   /* Render code... */
  }
}

选项2:

class Counter extends React.component {
  renderNumbers() {
    return <Text>...</Text>
  }
  render() {
   /* Render code... */
  }
}

您收到错误的原因是因为Javascript编译器认为您正在“调用”而不是“定义”renderNumbers()。所以...它会转到),并且需要换行符或;,但它会看到{并引发错误。

如果您使用选项2拨打this

,请不要忘记使用renderNumbers()关键字
相关问题