Javascript 清除本地文本输入
使用React Native中的Redux AddTodo示例。下面的第一个AddTodo示例使用state存储TextInput值,工作正常Javascript 清除本地文本输入,javascript,react-native,Javascript,React Native,使用React Native中的Redux AddTodo示例。下面的第一个AddTodo示例使用state存储TextInput值,工作正常 class AddTodo extends React.Component{ constructor(props){ super(props); this.state = { todoText: "" }; } update(e){ if(this.state.todoText.
class AddTodo extends React.Component{
constructor(props){
super(props);
this.state = { todoText: "" };
}
update(e){
if(this.state.todoText.trim()) this.props.dispatch(addTodo(this.state.todoText));
this.setState({todoText: "" });
}
render(){
return(
<TextInput
value = {this.state.todoText}
onSubmitEditing = { (e)=> { this.update(e); } }
onChangeText = { (text) => {this.setState({todoText: text}) } } />
);
}
}
是否有任何方法可以清除onSubmitEditing中的InputText值?将ref添加到您的TextInput中,例如:
<TextInput ref={input => { this.textInput = input }} />
{this.textInput=input}}/>
然后调用
this.textInput.clear()
清除iOS的输入值,它将给出默认的明文按钮
<TextInput clearButtonMode="always" />
请参见这对我很有用
ref={element => {
//Clear text after Input
this.attendee = element
}}
onSubmitEditing={this.handleAddPress}
和
this.attendee.setNativeProps({text:'})
//输入后清除文本我在React NativeOnSubmitEditing中将此代码用于清除文本输入
你可以查看我的零食:
代码如下:
状态={
搜索输入:“”,
clearInput:false
}
render(){
返回(
这是我的国家({
搜索输入框
})}
值={!this.state.clearInput?this.state.searchInput:null}
onSubmitEditing={()=>{
这是我的国家({
clearInput:!this.state.clearInput,
})
}}
/>
)
}
根据之后的更改和建议,您需要使用React.createRef在构造函数中检索ref: 在构造函数函数处:
this.myTextInput=React.createRef()代码>
在渲染功能中:
然后你可以打电话
this.myTextInput.current.clear();
[1] 以下代码示例:
<TextInput
onChangeText={(text) => this.onChangeText(text)}
ref={component => this._textInput = component}
onSubmitEditing={() => {
this.clearText()
}}
/>
clearText(){
this._textInput.setNativeProps({ text: ' ' });
setTimeout(() => {
this._textInput.setNativeProps({ text: '' });
},3);
}
this.onChangeText(text)}
ref={component=>this.\u textInput=component}
onSubmitEditing={()=>{
这是clearText()
}}
/>
明文(){
这是._textInput.setNativeProps({text:'});
设置超时(()=>{
这是._textInput.setNativeProps({text:'});
},3);
}
我使用的是本机base
下面是我如何使它工作的
constructor(props) {
super(props);
this.searchInput = React.createRef();
}
<Input
placeholder="Search"
ref={this.searchInput}
/>
参考资料这对我来说很有效
在构造函数处初始化myTextInput:
this.myTextInput = React.createRef();
在渲染函数中添加引用:
<Input ref={this.myTextInput} />
一种更简单的方法是使用TextInput
的value
属性,并使用组件的状态值对象设置TextInput的值
state = {
inputTextValue : '',
}
submitText = () => {
//handle the click action
//add this line at the end of the function after you are done handling with the input text value.
setState({inputTextValue : ''})
}
<TextInput
onChangeText={(text) => this.setState({ inputText: text })}
placeholder="Monday's breakfast"
value={this.state.inputTextValue}
/>
<TouchableOpacity
onPress={() => this.submitText()}>
<Text>Submit</Text>
</TouchableOpacity>
状态={
InputExtValue:“”,
}
submitText=()=>{
//处理单击操作
//处理完输入文本值后,在函数末尾添加此行。
setState({inputTextValue:''})
}
this.setState({inputText:text})}
占位符=“星期一的早餐”
value={this.state.inputExtValue}
/>
this.submitText()}>
提交
{this.name=input}
/>
this.name.clear();
this.email.clear();
此参数为.password.clear();
这是.confirmPassword.clear();
谢谢@AndréAbboud通过您的帮助,我可以清除我的TextInput字段,但根据我的自定义TextInput,我在实现上做了一些细微的更改
请检查用于实施的代码和方法。据我所知,现在我需要清除文本输入的要求已经完全满足,如果需要任何更改,请在评论中通知
我所做的就是让它发挥作用:
在setupSender.js中
。。。
此.state={
clearInput:false,
...
}
...
setupSenderSubmit=()=>{
...
这个.setState({
clearInput:!this.state.clearInput,
})
...
}
...
this.setState({firstName})}
clearInput={this.state.clearInput}
值={this.state.firstName}
returnKeyType={'next'}
自动对焦={true}
onSubmitEditing={()=>this.input2.current.focus()}
>
...
和CustomTextInput.js中的
this.state={
clearInput:this.props.clearInput,
}
...
静态getDerivedStateFromProps=(props,state)=>{
if(props.clearInput!=''| | props.clearInput!==未定义){
返回{
clearInput:props.clearInput
}
}
返回null;
}
...
{
this.setState({text});
this.props.onChangeText(文本)
}
}
...
此状态={
评论:'',
}
提交后
if(response.success)
{
this.commentMsg.clear();//TODO me
}
{
this.commentMsg=输入
}}
onChangeText={(text)=>this.setState({commentMsg:text})}
占位符='Comment'/>
在我的函数组件上,我调用另一个函数和submitHandler,该函数将负责清除文本
const [text, setText] = useState('');
const anotherFunc = (val) =>{
setText('');
}
return (
<View>
<TextInput
value ={text}
onChangeText ={changeHander}
placeholder = 'Add '
/>
<Button
title = "Add Something "
onPress = {()=> {submitHandler(text) , anotherFunc(text)}}
/>
</View>
)
const[text,setText]=useState(“”);
const anotherFunc=(val)=>{
setText(“”);
}
返回(
{submitHandler(文本),另一个func(文本)}
/>
)
使用本机基址中的输入组件对本机进行反应。
这对我很有用:
<Input ref={input => {this.textInput = input;}}
注意:不必使用React.createRef()进行初始化 因为您使用的是功能组件,所以可以按如下方式使用钩子。如果有条件呈现代码,请检查传递给useEffect的函数中是否定义了todoInput。我假设您的状态变量在依赖项列表中被称为todoText
import {useRef, useEffect} from 'react';
let AddTodo = ({ dispatch }) => {
const todoInput = useRef();
useEffect(()=>todoInput.current.clear(),[todoText]);
return (
<TextInput
ref={todoInput}
onSubmitEditing = { e => { dispatch(addTodo(e.nativeEvent.text)) } }
/>
)
}
从'react'导入{useRef,useffect};
让AddTodo=({dispatch})=>{
const todoInput=useRef();
useffect(()=>todoInput.current.clear(),[todoText]);
返回(
{dispatch(addTodo(e.nativeEvent.text))}
/>
)
}
为我工作
<TextInput
ref={ref => {
this.textInput = ref;
}}
...
/>
对于RN>0.6
const [msg, setMsg] = useState()
在TextInput中使用值
<TextInput
onChangeText={(txt) => setMsg(txt)}}
value={msg}
/>
您还可以将
的值设置为与状态相同的值,并在使用数据后,将状态设置回空字符串:
//state
const [state, setState] = useState({
name: '',
lastname: ''
})
//functions
const newUser = () => {
// Do your magic and after
setState({
name: '',
lastname: ''
})
}
const handleOnChange = () => {
setState({
// change your state
})
}
//render
<TextInput
value={state.name}
placeholder='Name'
onChangeText={/* execute your handleOnChange() */}
/>
<TextInput
value={state.lastname}
placeholder='Lastname'
onChangeText={/* execute your handleOnChange() */}
/>
<Button title='Saved' onPress={() => newUser()} />
//状态
常量[状态,设置状态]=使用状态({
名称:“”,
姓氏:“”
})
//功能
const newUser=()=>{
//做你的博士
<Input ref={input => {this.textInput = input;}}
this.textInput._root.clear();
import {useRef, useEffect} from 'react';
let AddTodo = ({ dispatch }) => {
const todoInput = useRef();
useEffect(()=>todoInput.current.clear(),[todoText]);
return (
<TextInput
ref={todoInput}
onSubmitEditing = { e => { dispatch(addTodo(e.nativeEvent.text)) } }
/>
)
}
<TextInput
ref={ref => {
this.textInput = ref;
}}
...
/>
clearMsg(){
this.textInput.state.value = ''
}
const [msg, setMsg] = useState()
<TextInput
onChangeText={(txt) => setMsg(txt)}}
value={msg}
/>
const pressButton = () => {
setMsg('')
}
//state
const [state, setState] = useState({
name: '',
lastname: ''
})
//functions
const newUser = () => {
// Do your magic and after
setState({
name: '',
lastname: ''
})
}
const handleOnChange = () => {
setState({
// change your state
})
}
//render
<TextInput
value={state.name}
placeholder='Name'
onChangeText={/* execute your handleOnChange() */}
/>
<TextInput
value={state.lastname}
placeholder='Lastname'
onChangeText={/* execute your handleOnChange() */}
/>
<Button title='Saved' onPress={() => newUser()} />