英文:
ERROR TypeError: text.split is not a function. (In 'text.split(/\s+/)', 'text.split' is undefined)
问题
App.js
const [text, setText] = React.useState('');
const handleOnChange = (text) => {
setText(text)
}
<View style={styles.textarea}>
<TextInput
placeholder="Enter Your text here!!"
placeholderTextColor='#666161'
textAlignVertical="top"
value={text}
multiline={true}
onChangeText={text => handleOnChange(text)} // Use onChangeText instead of onChange
style={{
padding: 15,
width: windowWidth / 1.10,
height: verticalScale(350),
borderColor: '#fff',
backgroundColor: '#f3f3f3',
borderWidth: 1,
borderRadius: scale(10),
fontSize: RFPercentage(3),
fontWeight: 'bold',
fontFamily: 'Roboto',
}}
/>
</View>
<Text style={styles.textsummy}>
{text.split(/\s+/).filter((element) => { return element.length !== 0 }).length} <Text style={{ color:'#E5E5E5', fontWeight:'normal' }}>words and</Text> {text.length} <Text style={{ color:'#E5E5E5', fontWeight:'normal' }}>characters</Text>
</Text>
Error:
ERROR TypeError: text.split is not a function. (In 'text.split(/\s+/)', 'text.split' is undefined)
在我的项目中,有一个文本区域和文本区域值存储在 text
状态中,我将在 text
状态中应用 split 函数,因为如果文本区域为空,则将计为单词或字符,如果输入一个单词并按回车,则将计为1个单词,而不是两个,只计算单词,而不计算空格。
但是,每当我尝试运行我的项目并在文本区域中输入文本时,都会出现如上所示的错误。
有人可以告诉我我犯了什么错误。
英文:
App.js
const [text, setText] = React.useState('');
const handleOnChange = (text) => {
setText(text)
}
<View style={styles.textarea}>
<TextInput
placeholder="Enter Your text here!!"
placeholderTextColor={'#666161'}
textAlignVertical="top"
value={text}
multiline={true}
onChange={(text)=>handleOnChange(text)}
style={{
padding: 15,
width:windowWidth/1.10,
height: verticalScale(350),
borderColor: '#fff',
backgroundColor: '#f3f3f3',
borderWidth: 1,
borderRadius: scale(10),
fontSize: RFPercentage(3),
fontWeight: 'bold',
fontFamily: 'Roboto',
}}
/>
</View>
<Text style={styles.textsummy}>{text.split(/\s+/).filter((element) => { return element.length !== 0 }).length} <Text style={{color:'#E5E5E5',fontWeight:'normal'}}>words and</Text> {text.length} <Text style={{color:'#E5E5E5',fontWeight:'normal'}}>characters</Text></Text>
Error:
ERROR TypeError: text.split is not a function. (In 'text.split(/\s+/)', 'text.split' is undefined)
In my project, one textarea and textarea value are stored in text
the state and I will apply the split function in text
the state
because it will count as a word or character if the textarea is null then the word is 0 or if you type one word and press enter then the word is 1, not a two in sort it only counts the word, not a blank space.
but whenever I try to run my project and type something on textarea view it's given me an error as given above.
anyone can tell me where I do a mistake
答案1
得分: 1
这可以通过两种方式解决:
方法1: 使用onChnageText:
onChangeText={(text) => handleOnChange(text)}
方法2:使用OnChange:
onChange={(event) => handleOnChange(event.nativeEvent.text)}
英文:
This can be solved in two ways :
Method 1 : Using onChnageText:
onChangeText={(text)=>handleOnChange(text)}
Method 2 : Using OnChange :
onChange={(event) => handleOnChange(event.nativeEvent.text)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论