反应本机 - 可按 - 无法运行示例代码



我正在尝试运行react native的示例代码,您可以在这里找到:https://reactnative.dev/docs/pressable

我一直收到错误消息:警告:React.createElement:type无效--应为字符串(用于内置组件(或类/函数(用于复合组件(,但得到:%s。%s%s,未定义,您可能忘记从定义的文件导出组件中,或者您可能混淆了默认导入和命名导入

有人能告诉我这里出了什么问题吗?

示例代码:

import React, { useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
const App = () => {
const [timesPressed, setTimesPressed] = useState(0);
let textLog = '';
if (timesPressed > 1) {
textLog = timesPressed + 'x onPress';
} else if (timesPressed > 0) {
textLog = 'onPress';
}
return (
<View>
<Pressable
onPress={() => {
setTimesPressed((current) => current + 1);
}}
style={({ pressed }) => [
{
backgroundColor: pressed
? 'rgb(210, 230, 255)'
: 'white'
},
styles.wrapperCustom
]}>
{({ pressed }) => (
<Text style={styles.text}>
{pressed ? 'Pressed!' : 'Press Me'}
</Text>
)}
</Pressable>
<View style={styles.logBox}>
<Text testID="pressable_press_console">{textLog}</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
text: {
fontSize: 16
},
wrapperCustom: {
borderRadius: 8,
padding: 6
},
logBox: {
padding: 20,
margin: 10,
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#f0f0f0',
backgroundColor: '#f9f9f9'
}
});
export default App;

Pressable在Expo中还不可用,因为Expo还不支持React Native v0.63。

相关内容

最新更新