簡體   English   中英

出現錯誤:文本字符串必須在 <Text> 零件

[英]getting error : Text String must be rendered within a <Text> component

我是新來的反應Native,出現錯誤

不變違規:文本字符串必須在Text組件內呈現。

到目前為止,除文本框和按鈕值外,我還沒有使用過文本字符串,但出現錯誤

我在下面分享我的代碼

import React from 'react';
import { StyleSheet, Text , View } from 'react-native';
import { TextInput , Button  , Appbar} from 'react-native-paper';



class App extends React.Component {


  state  = {
    fanme : '',
    mname : ''
  }

  render(){

    return(

     <View style={styles.container}>

     <Appbar.Header><Appbar.Content title="Calculate lOVE%"/>  </Appbar.Header>

      <TextInput label='fname' />
      <TextInput label='mname'  />

      <Button style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')}>
    Calculate 
  </Button>


    </View>

    );
  }
}

export default App;

const styles = StyleSheet.create({
  container : {
    flex : 1 ,
    backgroundColor : '#fff',
  },
});

在您的Button組件內部

<Button style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')}>
    Calculate // <-- ERROR
 </Button>

您放入Calculate字符串,可以將其與<Text> Calculate </Text>結合在一起,或者使用帶有title="Clear button"的button的本機屬性,這樣您的按鈕組件將像這樣結束:

 <Button title="Calculate" style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')}/>

在本機按鈕中,使用title屬性而不是按鈕文本:

<Appbar.Header><Appbar.Content title="Calculate lOVE%"/></Appbar.Header>
<TextInput label='fname' />
<TextInput label='mname'  />
<Button title="Calculate" style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')}></Button>

更多詳細信息請參見https://facebook.github.io/react-native/docs/button.html#title

正如React Native所說,文本應在Text Component中呈現! 這是您的問題:

<Button style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')}>
   Calculate 
</Button>

應更改為:

<Button style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')}>
   <Text> Calculate </Text>
</Button>

或像這樣使用Button的標題道具:

<Button title='Calculate' style={{margin : 10}} icon="add-a-photo" mode="contained" onPress={() => console.log('Pressed')} />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM