[英]React Native touchableopacity onpress not working
我是本機(或與此相關的任何JS框架)做出反應的新手,我正在嘗試制作一個簡單的點擊游戲,其中,一旦您點擊藍色方塊,它會隨機渲染到其他位置,然后再次點擊,以此類推。 到目前為止,這是我的代碼:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
TouchableOpacity,
View
} from 'react-native';
import styles from './src/styles/style';
export default class App extends Component<{}> {
constructor(props) {
super();
this.state = {
showStartButton: true, // change back to true
score: 0
}
}
startGame() {
this.setState({
showStartButton: !this.state.showStartButton
});
this.renderFirstStage();
}
score() {
this.setState({
showStartButton: !this.state.showStartButton,
score: score+1
});
}
renderStartButton() {
return (
<TouchableOpacity style={styles.startButton} onPress={()=>this.startGame()}>
<Text style={styles.startButtonText}>START</Text>
</TouchableOpacity>
);
}
renderWhiteBox() {
return (
<View style={{flex: 1, backgroundColor: 'white'}} />
);
}
renderBlueBox() {
return (
<View style={{flex: 1, backgroundColor: 'blue'}} />
);
}
renderFirstStage() {
var blueColNum = Math.floor(Math.random() * 6);
var blueRowNum = Math.floor(Math.random() * 4);
var col1 = ['white', 'white', 'white', 'white', 'white', 'white'];
var col2 = ['white', 'white', 'white', 'white', 'white', 'white'];
var col3 = ['white', 'white', 'white', 'white', 'white', 'white'];
var col4 = ['white', 'white', 'white', 'white', 'white', 'white'];
if (blueRowNum == 0)
col1[blueColNum] = 'blue';
else if (blueRowNum == 1)
col2[blueColNum] = 'blue';
else if (blueRowNum == 2)
col3[blueColNum] = 'blue';
else if (blueRowNum == 3)
col4[blueColNum] = 'blue';
return (
<View style={{flex: 1, flexDirection: 'row'}}>
<View style={{flex: 1}}>
<View style={styles.scoreBoard}><Text style={styles.timerText}>Time: {blueRowNum}</Text></View>
{col1.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()} />
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'gray'}} />
{col2.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()}/>
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
<View style={{flex: 1}}>
<View style={{flex: 1, backgroundColor: 'gray'}} />
{col3.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()} />
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
<View style={{flex: 1}}>
<View style={styles.scoreBoard}><Text style={styles.timerText}>Score: {this.state.score}</Text></View>
{col4.map(function(el){
if (el == 'blue')
return (
<TouchableOpacity style={{flex: 1, backgroundColor: el}} onPress={()=>this.score()} />
);
else
return (
<View style={{flex: 1, backgroundColor: el}} />
);
})}
</View>
</View>
);
}
render() {
return (
<View style={styles.container}>
{this.state.showStartButton ? this.renderStartButton() : null}
{!this.state.showStartButton ? this.renderFirstStage() : null}
</View>
);
}
}
因此,這是很多代碼,但為了上下文,我想將其全部包括在內。 無論如何,我遇到的問題是我的renderFirstStage()函數。 在return語句中看到我正在調用onPress事件處理程序以運行功能this.score的地方嗎? 好的,所以當我運行程序並點擊其中一個TouchableOpacities時,我看到一個錯誤消息:_this5.score不是函數... _this5.score是未定義的。 我最好的猜測是,由於我是從JSX內的JS代碼中調用onPress = {{()=> this.score}的,因此我無法以某種方式訪問App類中的方法。 因此,我認為范圍存在問題,或者可能不是,我不知道。 即使我的直覺是正確的,我也不知道如何糾正它。 如何從TouchableOpacity組件內部調用score方法? 我嘗試過的所有方法似乎對我都不起作用...
注意:我在MacBook Pro的iPhone 7模擬器上運行。
是的,它存在范圍問題,您需要在constructor
function
中this
function
與this
上下文bind
constructor(props) {
super();
this.state = {
showStartButton: true, // change back to true
score: 0
}
this.renderFirstStage = this.renderFirstStage.bind(this);
}
或者您可以簡單地使用call
來立即調用該function
並將其與this
綁定
{!this.state.showStartButton ? this.renderFirstStage.call(this) : null}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.