In my project this
is no longer bound when using arrow functions after upgrading to React Native 26.
If I don't use a .babelrc
in the example below it works with arrow functions. When a .babelrc
is added arrow functions no longer work.
.babelrc
{
"passPerPreset": true,
"presets": [
{"plugins":["../schema/babelRelayPlugin"]},
"react-native",
]
}
I also tried:
{
"passPerPreset": true,
"presets": [
{"plugins":["../schema/babelRelayPlugin"]},
"react-native-stage-0",
]
}
and
{
"passPerPreset": true,
"presets": [
{"plugins":["../schema/babelRelayPlugin"]},
"react-native",
{"plugins":["transform-es2015-arrow-functions"]},
]
}
This errors
class NoArrow extends Component {
constructor(){
super();
this.state={x:0};
}
inc = ()=>{
this.setState({x:this.state.x+1});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={this.inc}>
Welcome to React Native! {this.state.x}
</Text>
</View>
);
}
}
This works
class NoArrow extends Component {
constructor(){
super();
this.state={x:0};
this.inc=this.inc.bind(this);
}
inc(){
this.setState({x:this.state.x+1});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={this.inc}>
Welcome to React Native! {this.state.x}
</Text>
</View>
);
}
}
When adding/removing .babelrc
also run:
- watchman watch-del-all
- npm start --reset-cache
- I also edit the file (adding a comment) to make sure it recompiles.
Side note: funny thing is this works even with the .babelrc
class NoArrow extends Component {
constructor(){
super();
this.state={x:0};
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} onPress={()=> this.setState({x:this.state.x+1})}>
Welcome to React Native! {this.state.x}
</Text>
</View>
);
}
}
Update 1
Inside $TMPDIR delete the cached file; it has a hashed name like 11acb28f1c8d3c6313ca5f8ccba3c158
Using react-native-stage-0
might have fixed the arrow function issue, but now Relay.QL are no longer compiled correctly.
{
"passPerPreset": true,
"presets": [
{"plugins":["../schema/babelRelayPlugin"]},
"react-native-stage-0"
]
}