admin管理员组文章数量:1424668
I tried to code in react native today and tried to write a program with a button and a text that changes with every press:
import { Text, View, StyleSheet, TouchableOpacity, Button, TextInput } from 'react-native';
import Constants from 'expo-constants';
export default () => {
const onButton = () =>
{
myResult(10);
}
return (
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
)
}
It gives me this error:
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
If I ment the text class or the button class like that:
//<Text>Hello World</Text>
the other class would work.
Any suggestions?
I tried to code in react native today and tried to write a program with a button and a text that changes with every press:
import { Text, View, StyleSheet, TouchableOpacity, Button, TextInput } from 'react-native';
import Constants from 'expo-constants';
export default () => {
const onButton = () =>
{
myResult(10);
}
return (
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
)
}
It gives me this error:
Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>?
If I ment the text class or the button class like that:
//<Text>Hello World</Text>
the other class would work.
Any suggestions?
Share Improve this question asked Jan 6, 2021 at 20:41 CheeseBurgerCheeseBurger 472 silver badges5 bronze badges 2- 1 The error message that you quote is very explicit about both what the problem is, and how to fix it. Could you expand on which part of it you don't understand? – Robin Zigmond Commented Jan 6, 2021 at 20:45
-
Besides the two answers below, I think you'll also need to replace
onPress={onButton}
byonPress={()=>{onButton()}}
, otherwise your function will be called even before the button is clicked. – Maf Commented Jan 6, 2021 at 23:47
3 Answers
Reset to default 4You can't render multiple ponent like that. Like the error message said, you have to wrap them in an enclosing tag.
<View>
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
</View>
You are returning 2 ponents, in react you must return just one ponent, so your code you could wrapping in two ways:
<View>
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
</View>
Or using Fragment
ponent of react that will no add a node in your ponent tree
//You can use Fragment as <></> or <React.Fragment></React.Fragment>
<>
<Text>Hello World</Text>
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
</>
The best option is to wrap it in a <View>
tag, as mentioned in the other two answers.
Here is a fun solution, you can even return an array.
Example:
return [
<Text>Hello World</Text>,
<Button
onPress={onButton}
title="Click Here to calculate"
color="black"
/>
];
本文标签: javascriptShow button and text in react nativeStack Overflow
版权声明:本文标题:javascript - Show button and text in react native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744692733a2620092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论