我正在尝试使用Reacti原住民构建一个应用程序。我做了一个自定义按钮,onPress不起作用,试图寻找答案,但找不到答案,代码看起来就像文档(从那里复制)。
这是自定义按钮:
import React from 'react';
import { View, Text, StyleSheet, TouchableHighlight, TouchableOpacity } from 'react-native';
import COLORS from '../constants/constants';
class AppButton extends React.Component {
render() {
return(
<View style={styles.container}>
<TouchableOpacity>
<Text style={styles.title}>
{this.props.title}
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: COLORS.appOrange,
borderRadius: 20,
elevation: 3,
flexShrink: 0,
width: '100%',
height: 60,
justifyContent: 'center'
},
title: {
fontSize: 24,
color: COLORS.appWhite,
textAlign: 'center',
},
});
export default AppButton;以下是我尝试使用onPress的方式:
import React from 'react';
import { View, Text, StyleSheet, TextInput } from 'react-native';
import COLORS from '../constants/constants';
import Card from '../components/card';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import AppTextInput from '../components/appTextInput';
import AppButton from '../components/appButton';
class Login extends React.Component {
onPress = () => {
console.log("hello world!@#!@#@!#");
// this.setState({
// count: this.state.count + 1
// });
};
render() {
return(
<View style={styles.superContainer}>
<View style={styles.formContainer}>
<AppTextInput placeHolderText="Email@Address.com"></AppTextInput>
<AppTextInput placeHolderText="Passwrod"></AppTextInput>
</View>
<View style={styles.buttonContainer}>
<AppButton
title="LOGIN"
style={styles.loginButton}
onPress={this.onPress}
/>
</View>
</View>
);
}
}我试着看了看控制台,但没有打印出来,尝试了几个不同的例子,我在互联网上看到,但点击就是没有注册。
发布于 2020-03-31 17:36:52
我将OnPress方法作为道具传递给AppButton,并将此方法附加到TouchableOpcaity OnPress事件中。
import React from 'react';
import { View, Text, StyleSheet, TouchableHighlight, TouchableOpacity } from 'react-native';
import COLORS from '../constants/constants';
class AppButton extends React.Component {
render() {
return(
<View style={styles.container}>
<TouchableOpacity onPress={this.props.handleOnPress}>
<Text style={styles.title}>
{this.props.title}
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: COLORS.appOrange,
borderRadius: 20,
elevation: 3,
flexShrink: 0,
width: '100%',
height: 60,
justifyContent: 'center'
},
title: {
fontSize: 24,
color: COLORS.appWhite,
textAlign: 'center',
},
});
export default AppButton; import React from 'react';
import { View, Text, StyleSheet, TextInput } from 'react-native';
import COLORS from '../constants/constants';
import Card from '../components/card';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import AppTextInput from '../components/appTextInput';
import AppButton from '../components/appButton';
class Login extends React.Component {
onPress = () => {
console.log("hello world!@#!@#@!#");
// this.setState({
// count: this.state.count + 1
// });
};
render() {
return(
<View style={styles.superContainer}>
<View style={styles.formContainer}>
<AppTextInput placeHolderText="Email@Address.com"></AppTextInput>
<AppTextInput placeHolderText="Passwrod"></AppTextInput>
</View>
<View style={styles.buttonContainer}>
<AppButton
title="LOGIN"
style={styles.loginButton}
handleOnPress={this.onPress}
/>
</View>
</View>
);
}
}发布于 2020-03-31 17:40:16
在您的示例中,您没有将onPress道具传递到自定义按钮组件中的TouchableOpacity中。
尝尝这个
定制按钮
import React from "react";
import {
View,
Text,
StyleSheet,
TouchableHighlight,
TouchableOpacity
} from "react-native";
import COLORS from "../constants/constants";
class AppButton extends React.Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this.props.onPress}>
<Text style={styles.title}>{this.props.title}</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: COLORS.appOrange,
borderRadius: 20,
elevation: 3,
flexShrink: 0,
width: "100%",
height: 60,
justifyContent: "center"
},
title: {
fontSize: 24,
color: COLORS.appWhite,
textAlign: "center"
}
});
export default AppButton;登录类
import React from "react";
import { View, Text, StyleSheet, TextInput } from "react-native";
import COLORS from "../constants/constants";
import Card from "../components/card";
import { Colors } from "react-native/Libraries/NewAppScreen";
import AppTextInput from "../components/appTextInput";
import AppButton from "../components/appButton";
class Login extends React.Component {
onPress = () => {
console.log("hello world!@#!@#@!#");
// this.setState({
// count: this.state.count + 1
// });
};
render() {
return (
<View style={styles.superContainer}>
<View style={styles.formContainer}>
<AppTextInput placeHolderText="Email@Address.com"></AppTextInput>
<AppTextInput placeHolderText="Passwrod"></AppTextInput>
</View>
<View style={styles.buttonContainer}>
<AppButton
title="LOGIN"
style={styles.loginButton}
onPress={this.onPress}
/>
</View>
</View>
);
}
}发布于 2020-03-31 17:59:01
您确定"this“动词指的是正确的对象(类实例)吗?
让我们来看看这个:
class Login extends React.Component {
const onPress = () => {
console.log("the world is not enough");
};
render() {
/* assign this class instance to that reference */
const that = this;
return(
...
<View style={styles.buttonContainer}>
<AppButton
title="LOGIN"
style={styles.loginButton}
/* use that class reference */
onPress={that.onPress}
/>
</View>
...
);
}
}https://stackoverflow.com/questions/60956513
复制相似问题