React-native(ES5)to(ES6)

时间:2016-04-21 17:11:40

标签: syntax-error react-native ecmascript-6

这个ES6在React native中给出了一些问题。我想在纯ES6中编写代码,但编写部分会出错。

ES5代码

renderScene: function (route, navigator) {
       var Component = route.component;
       return (
           <Component openModal={() => this.setState({modal: true})}/>
       )
   },

ES6在哪里:

renderScene(route, navigator) {
    var Component = route.component;
    return (
        <Component openModal={() => this.setState({modal: true})  }/>
    )
}

我收到此错误:

enter image description here

我试图添加bind(this),但它不起作用。

有人可以帮忙吗? Thx提前

编辑:添加了完整的班级代码

class Navigation extends Component {
    constructor(props) {
        super(props)
        this.state = {
            modal: false,
        }
    }

    renderScene(route, navigator) {
        var Component = route.component;
        return (
            <Component openModal={() => this.setState({modal: true})  }/>
        )
    }

    goToOtherRoute() {
        //this.refs.navigator.push({newRoute})
    }

    render() {
        return (
            <View style={styles.container}>
                <Navigator
                    ref="navigator"
                    initialRoute={RouteStack.app}
                    renderScene={this.renderScene}
                />
                {this.state.modal ? <Basket goToOtherRoute={this.goToOtherRoute} closeModal={() => this.setState({modal: false}) }/> : null }
            </View>
        );
    }
}

2 个答案:

答案 0 :(得分:4)

像这样声明renderScene:

renderScene = (route, navigator) => {
  //code
}

还有其他选项,我已将它们写下here

答案 1 :(得分:0)

当然这里是: 对于extends Component

是的
storeBusiness.load({addRecords: true});