Javascript 如何在react native中清除TextInput值?
我请求你的帮助,因为我正在为这条消息的目的而挣扎。我想知道,如果您正在开发一个不需要传输很多数据的小型应用程序,那么在react native中不使用“state”是否是一种很好的做法 我的问题是:Javascript 如何在react native中清除TextInput值?,javascript,react-native,Javascript,React Native,我请求你的帮助,因为我正在为这条消息的目的而挣扎。我想知道,如果您正在开发一个不需要传输很多数据的小型应用程序,那么在react native中不使用“state”是否是一种很好的做法 我的问题是: export default function ajouterJoueur ({navigation}) { let saisie = ''; /*Récupération de la donnée partieCourante*/ const partieCourante
export default function ajouterJoueur ({navigation})
{
let saisie = '';
/*Récupération de la donnée partieCourante*/
const partieCourante = DataNavigation.getData('partie');
return(
<View style={styles.container}>
<Text style={styles.titre}>Liste des joueurs ajoutés</Text>
<View>
{partieCourante.afficherListeJoueur()}
</View>
<View styles={styles.groupeBouton}>
<TextInput style = {styles.input}
placeholder = "Saisir le nom du joueur"
onChangeText = {(text) => saisie = text}
></TextInput>
<Button
title="Valider"
onPress={() => ajouterJoueurListe(navigation, saisie, partieCourante)}/>
</View>
<View style= {styles.lancementPartie}>
<Button
title="Lancer la partie"
onPress={() => navigation.navigate('AfficherPartie', {partieCourante})}/>
</View>
</View>
)
}
导出默认函数ajoterjouer({navigation})
{
让saisie='';
/*第三方保险*/
const partiecourant=DataNavigation.getData('partie');
返回(
阿朱特之歌
{partiecourant.afficherlisteJouer()}
saisie=text}
>
AjouterJouerListe(导航、安全、部分保险)}/>
navigation.navigate('AfficherPartie',{partieCourante}}/>
)
}
当屏幕重新加载此部件时,我想清除“TextInput”的值:
function ajouterJoueurListe(navigation, saisie, partieCourante)
{
let longueurNomSaisie = saisie.length;
console.log("Longueur du nom saisie : " + longueurNomSaisie);
if(longueurNomSaisie >= 1 && longueurNomSaisie < 10)
{
console.log(saisie);
//On crée le nouveau joueur et on initialise son score pour afficher ensuite ses informations.
let joueurCourant = new Joueur();
joueurCourant.ajouterNom(saisie);
joueurCourant.loggerInfoJoueur();
//On ajoute le joueur à la partie et on affiche les informations
partieCourante.ajouterJoueur(joueurCourant);
partieCourante.loggerListeJoueur();
ajouterJoueur(navigation);
}
else
{
console.log("La longueur du nom ne convient pas.");
Alert.alert('Nom invalide', 'Longueur attendue entre 1 et 10 caractères.')
}
}
功能ajouterJoueurListe(导航、安全、部分保险)
{
让longueurNomSaisie=saisie.length;
控制台日志(“Longueur du nom saisie:+longueurNomSaisie”);
如果(longueurNomSaisie>=1和&longueurNomSaisie<10)
{
控制台日志(saisie);
//在《新时尚》杂志上,我们将为您提供最新的信息。
让JouerCourant=新Jouer();
阿约特南(塞西);
loggerinfojouer();
//关于信息的附加信息
部分保险公司(JouerCourant);
partiecourant.loggerlistejouer();
Ajouterjouer(导航);
}
其他的
{
console.log(“名称长而不方便”);
警报。警报('Nom invalide'、'Longueur ATENTDUE entre 1和10克拉)
}
}
我不明白为什么当我调用'aJouterJouer(导航)'时,InputText仍然填充着重新加载之前键入的值
谢谢你的帮助 首先,您需要引用您的TextInput,然后在需要它的地方可以调用.clear()来清除它
<TextInput ref={input => { this.textInput = input }} />
TextInput
是一个受控组件,这意味着如果提供,本机值将被强制与此值匹配。您只需向它传递一个值道具
import React, { Component } from 'react';
import { TextInput, Button } from 'react-native';
export default function UselessTextInput() {
const [value, onChangeText] = React.useState('Useless Placeholder'); // tracks the value of the text input.
const clearInput = React.useCallback(()=> onChangeText(''), []); // sets value to empty string
// you can use the same pattern whenever you want to clear the contents of the text input
return (
<>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
/>
<Button title="clear" onPress={clearInput} />
</>
);
}
import React,{Component}来自'React';
从“react native”导入{TextInput,Button};
导出默认函数UselessTextInput(){
const[value,onChangeText]=React.useState(‘无用占位符’);//跟踪文本输入的值。
const clearInput=React.useCallback(()=>onChangeText(“”),[]);//将值设置为空字符串
//只要您想清除文本输入的内容,就可以使用相同的模式
返回(
onChangeText(文本)}
value={value}
/>
);
}
你做到了吗?答案有用吗?请提供一些反馈。
import React, { Component } from 'react';
import { TextInput, Button } from 'react-native';
export default function UselessTextInput() {
const [value, onChangeText] = React.useState('Useless Placeholder'); // tracks the value of the text input.
const clearInput = React.useCallback(()=> onChangeText(''), []); // sets value to empty string
// you can use the same pattern whenever you want to clear the contents of the text input
return (
<>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
onChangeText={text => onChangeText(text)}
value={value}
/>
<Button title="clear" onPress={clearInput} />
</>
);
}