(TypeScript,原生反应)setTimeout 与 onPressIn 和 onPressOut 组合
我是 React Native 的新手,正在构建我的第一个应用程序。 我有一个按钮组件,按下时会执行某些操作,按住时会执行相同但重复的操作。为了实现这一目标,我构建了一个简单的逻辑,但我无法让它正常工作,因为当我按住按钮时,它会执行它应该执行的操作,但控制台显示:
“请为该组件附加一个方法”
当我松开按钮时会显示该消息,因此我认为问题出在 onPressOut 中。
这是代码,对于代码中的西班牙语感到抱歉:c
import { View, Text, StyleSheet } from "react-native";
import { Button } from "react-native-elements";
interface INTERFACE {
accionMenos: () => void;
accionMas: () => void;
texto: string;
titleBotonMenos: string;
titleBotonMas: string;
}
const BotonTextoBoton = ({
accionMenos,
accionMas,
texto,
titleBotonMenos,
titleBotonMas,
}: INTERFACE) => {
let timer: NodeJS.Timeout;
function addTimer(action: () => void) {
action();
timer = setTimeout(() => {
addTimer(action);
}, 100);
}
function eraseTimer() {
clearTimeout(timer);
}
return (
<View style={styles.viewContainer}>
<View style={{ width: "15%", backgroundColor: "red" }}>
<Button
onPressIn={() => addTimer(accionMenos)}
onPressOut={eraseTimer}
title={titleBotonMenos}
/>
</View>
<Text style={{ margin: 3, fontSize: 20 }}>{texto}</Text>
<View style={{ width: "15%" }}>
<Button
onPressIn={() => addTimer(accionMas)}
onPressOut={eraseTimer}
title={titleBotonMas}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
viewContainer: {
flexDirection: "row",
justifyContent: "center",
paddingTop: 30,
alignItems: "center",
},
});
export default BotonTextoBoton;
I'm new to React Native and I'm building my first app.
I have a button component which when pressed does something and when held pressed does the same but repeatedly. To achieve that I built a simple logic but I can't get it to work properly since when I hold the button pressed it does what it should but the console says:
"Please attach a method to this component"
That message displays when I let the button go so I think the problem is in the onPressOut.
this is the code, sorry about the spanish lenguage in the code :c
import { View, Text, StyleSheet } from "react-native";
import { Button } from "react-native-elements";
interface INTERFACE {
accionMenos: () => void;
accionMas: () => void;
texto: string;
titleBotonMenos: string;
titleBotonMas: string;
}
const BotonTextoBoton = ({
accionMenos,
accionMas,
texto,
titleBotonMenos,
titleBotonMas,
}: INTERFACE) => {
let timer: NodeJS.Timeout;
function addTimer(action: () => void) {
action();
timer = setTimeout(() => {
addTimer(action);
}, 100);
}
function eraseTimer() {
clearTimeout(timer);
}
return (
<View style={styles.viewContainer}>
<View style={{ width: "15%", backgroundColor: "red" }}>
<Button
onPressIn={() => addTimer(accionMenos)}
onPressOut={eraseTimer}
title={titleBotonMenos}
/>
</View>
<Text style={{ margin: 3, fontSize: 20 }}>{texto}</Text>
<View style={{ width: "15%" }}>
<Button
onPressIn={() => addTimer(accionMas)}
onPressOut={eraseTimer}
title={titleBotonMas}
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
viewContainer: {
flexDirection: "row",
justifyContent: "center",
paddingTop: 30,
alignItems: "center",
},
});
export default BotonTextoBoton;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Button
组件需要一个onPress
方法。这可能什么也不做。比较以下代码片段以使其更加明确。不提供onPress
相当于传递undefined
。这会产生错误消息
请向此组件附加一个方法
。您可以按如下方式修复此问题。
我们只提供函数
() =>; {}
不执行任何操作。该消息不再出现。The
Button
component requires aonPress
method. This could be doing nothing. Compare the following code snippet to make this more explicit. Providing noonPress
is equivalent with passingundefined
.This yields the error message
Please attach a method to this component
.You can fix this as follows.
We just provide the function
() => {}
which does nothing. The message does no longer occur.