ERROR TypeError: text.split is not a function. (In ‘text.split(/\s+/)’, ‘text.split’ is undefined)

huangapple go评论70阅读模式
英文:

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(&#39;&#39;);

const handleOnChange = (text) =&gt; {
  setText(text)
}

&lt;View style={styles.textarea}&gt;
            &lt;TextInput
              placeholder=&quot;Enter Your text here!!&quot;
              placeholderTextColor={&#39;#666161&#39;}
              textAlignVertical=&quot;top&quot;
              value={text}
              multiline={true}
              onChange={(text)=&gt;handleOnChange(text)}
              style={{
                padding: 15,
                width:windowWidth/1.10,
                height: verticalScale(350),
                borderColor: &#39;#fff&#39;,
                backgroundColor: &#39;#f3f3f3&#39;,
                borderWidth: 1,
                borderRadius: scale(10),
                fontSize: RFPercentage(3),
                fontWeight: &#39;bold&#39;,
                fontFamily: &#39;Roboto&#39;,
              }}
            /&gt;
          &lt;/View&gt;
           &lt;Text style={styles.textsummy}&gt;{text.split(/\s+/).filter((element) =&gt; { return element.length !== 0 }).length} &lt;Text style={{color:&#39;#E5E5E5&#39;,fontWeight:&#39;normal&#39;}}&gt;words and&lt;/Text&gt; {text.length} &lt;Text style={{color:&#39;#E5E5E5&#39;,fontWeight:&#39;normal&#39;}}&gt;characters&lt;/Text&gt;&lt;/Text&gt;

Error:

 ERROR  TypeError: text.split is not a function. (In &#39;text.split(/\s+/)&#39;, &#39;text.split&#39; 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)=&gt;handleOnChange(text)}

Method 2 : Using OnChange :

onChange={(event) =&gt; handleOnChange(event.nativeEvent.text)}

huangapple
  • 本文由 发表于 2023年2月19日 15:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定