【发布时间】:2020-08-13 00:11:03
【问题描述】:
Here's a video of the button not clicking in action.
我正在关注React Native Docs 并且有一个非常简单的设置:
import React, { useState } from "react";
import { View, Text, Image, ScrollView, TextInput, Button } from "react-native";
const Styles = {
image: { width: 200, height: 200 },
};
const Cat = () => {
const [isHungry, setIsHungry] = useState(true);
return (
<View>
<Image
source={{
uri: "https://reactnative.dev/docs/assets/p_cat2.png",
}}
style={Styles.image}
/>
<Button
onPress={() => {
console.log("Pressed!");
setIsHungry(false);
}}
disabled={!isHungry}
title={isHungry ? "Milk please!" : "Thanks!"}
/>
</View>
);
};
const App = () => {
return (
<>
<Cat />
</>
);
};
export default App;
这是在我的安卓手机 (OnePlus 6) 上运行,使用 Expo。
当我按下按钮时,我得到了点击交互,但没有调用 onPress 函数。
即使我清除所有内容并直接在文档页面上使用代码:
import React, { useState } from "react";
import { Button, Text, View } from "react-native";
const Cat = (props) => {
const [isHungry, setIsHungry] = useState(true);
return (
<View>
<Text>
I am {props.name}, and I am {isHungry ? "hungry" : "full"}!
</Text>
<Button
onPress={() => {
setIsHungry(false);
}}
disabled={!isHungry}
title={isHungry ? "Pour me some milk, please!" : "Thank you!"}
/>
</View>
);
}
const Cafe = () => {
return (
<>
<Cat name="Munkustrap" />
<Cat name="Spot" />
</>
);
}
export default Cafe;
这里发生了什么?供参考:
- 我正在使用最新的 React Native / Expo 版本
- RNDebugger 已打开,是我的主要重点标签
- 我已多次刷新/重新加载应用,但无济于事
- 按了 20 次后,我终于可以按下来开火了
知道我在这里做错了什么吗?!看起来很基本,但非常令人沮丧。
【问题讨论】:
-
似乎没有什么不寻常的。尝试关闭调试器并重新启动捆绑器
-
原来是某种世博会问题;重新启动调试器,我的手机就解决了这个问题。
标签: reactjs react-native