Javascript 在Redux/React中设置/清除输入
我想知道当使用react时,如何通过redux在一个清晰的字段中连接。 例如,我有一个带有一些示例代码的表单Javascript 在Redux/React中设置/清除输入,javascript,forms,reactjs,redux,redux-form,Javascript,Forms,Reactjs,Redux,Redux Form,我想知道当使用react时,如何通过redux在一个清晰的字段中连接。 例如,我有一个带有一些示例代码的表单 <Field component={datewidget} id ="date" name="date" type="date" label="date" /> <button type="button"onClick={() => {(clearMyInput());}}> </button> 我的问题是,如何通过单击按钮并将输入值设置为
<Field component={datewidget} id ="date" name="date" type="date" label="date" />
<button type="button"onClick={() => {(clearMyInput());}}>
</button>
我的问题是,如何通过单击按钮并将输入值设置为无来清除输入字段
例如,在jquery中,我可以编写如下内容:
$("#button").click(function () {
$("#date").val("");
});
我想知道如何在react中使用redux表单来完成此操作。在
字段的组件中,从存储中传递属性值
,并附加事件处理程序以分派更改
<Field
component={datewidget}
id="date"
name="date"
type="date"
label="date"
value={this.props.fieldValue}
onChange={this.props.changeFieldValue}
/>
然后使用分派clearDate
函数,您只需将所有表单的值设置为'
。我建议您在不使用样板文件的情况下查看哪些操作都可以完成。在您的字段的组件中,从存储中传递属性值
,并附加事件处理程序以分派更改
<Field
component={datewidget}
id="date"
name="date"
type="date"
label="date"
value={this.props.fieldValue}
onChange={this.props.changeFieldValue}
/>
然后使用分派clearDate
函数,您只需将所有表单的值设置为'
。我建议您在不使用样板文件的情况下,研究一下哪种方法可以完成这些工作。我想补充一下现有答案,即有用于清除表单的内置功能:
<button type="button" onClick={reset}>
Clear Values
</button>
明确的价值观
注意这里的{reset}
onClick
操作处理程序。这是随Redux Form
一起提供的,作为对现有答案的补充,我想补充一点,即有用于清除表单的内置功能:
<button type="button" onClick={reset}>
Clear Values
</button>
import { reset, destroy } from 'redux-form'
//
//..code Block
render() {
const { resetForm, destroyForm } = this.props
return <ComponentName {...{ resetForm, destroyForm }} {...this.props} />
}
}
// @formName: for global use(in case if you render more than one form from this container..)
const mapDispatchToProps = dispatch => ({
resetForm: formName => dispatch(reset(formName)),
destroyForm: formName => dispatch(reset(formName)),
})
明确的价值观
注意这里的{reset}
onClick
操作处理程序。这是从“Redux Form”中导入{reset,destroy}时附带的
import { reset, destroy } from 'redux-form'
//
//..code Block
render() {
const { resetForm, destroyForm } = this.props
return <ComponentName {...{ resetForm, destroyForm }} {...this.props} />
}
}
// @formName: for global use(in case if you render more than one form from this container..)
const mapDispatchToProps = dispatch => ({
resetForm: formName => dispatch(reset(formName)),
destroyForm: formName => dispatch(reset(formName)),
})
//
//..代码块
render(){
const{resetForm,destroyForm}=this.props
返回
}
}
//@formName:供全局使用(如果您从该容器中呈现多个表单..)
const mapDispatchToProps=调度=>({
resetForm:formName=>dispatch(reset(formName)),
destroyForm:formName=>dispatch(重置(formName)),
})
现在从组件中调用它
const ComponentName = ({ resetForm, destroyForm }, ...props) => {
//..code Block
<button type='submit' onClick={() => resetForm('formName') && destroyForm('formName')} > Clear </button>
}
const ComponentName=({resetForm,destromform},…props)=>{
//..代码块
resetForm('formName')&&destroyForm('formName')}>Clear
}
快乐编码..从“redux表单”导入{reset,destroy}
//
//..代码块
render(){
const{resetForm,destroyForm}=this.props
返回
}
}
//@formName:供全局使用(如果您从该容器中呈现多个表单..)
const mapDispatchToProps=调度=>({
resetForm:formName=>dispatch(reset(formName)),
destroyForm:formName=>dispatch(重置(formName)),
})
现在从组件中调用它
const ComponentName = ({ resetForm, destroyForm }, ...props) => {
//..code Block
<button type='submit' onClick={() => resetForm('formName') && destroyForm('formName')} > Clear </button>
}
const ComponentName=({resetForm,destromform},…props)=>{
//..代码块
resetForm('formName')&&destroyForm('formName')}>Clear
}
快乐编码..您需要将表单输入存储在redux中吗?一般来说,我建议在提交之前将用户输入保存在字段中或本地组件状态。如果您必须使用redux,以便其他组件能够响应,只需确保从redux状态设置输入值,这样,如果通过分派清除该状态,输入也将被清除。(或者只给字段一个ref
并用refs.myInput.value=''
清除它)不确定这是否是您要问的,但听起来您只是缺少响应clearDate
操作的redux减速机。通常(如果您使用的是redux而不是本地状态),您会通过connect
将存储中的值绑定到组件,然后分派clearmyinport
操作。reducer将通过重置存储中的值来响应,这将重置以下形式的值:…clearDate(datedata,value)
-这些值来自哪里?你的意思是clearMyInput:(datedata,value)=>{
?值“datedata,value”没有包含在此代码示例中。很抱歉。基本上,在我提交此redux表单后,表单现在填充了输入中的值。此时,我希望单击按钮可以清除输入字段。是否需要将表单输入存储在redux中?通常我建议只保留用户inp在提交之前,在字段中或本地组件状态下执行ut。如果必须使用redux以便其他组件可以响应,请确保从redux状态设置输入值,以便如果通过分派清除该状态,输入也将被清除。(或者只给字段一个ref
并用refs.myInput.value=''
清除它)不确定这是否是您要问的,但听起来您只是缺少响应clearDate
操作的redux减缩器。通常(如果您使用的是redux而不是本地状态)您可以通过connect
将存储中的一个值绑定到组件,然后发送clearMyInput
操作。减速机将通过重置存储中的该值来响应,这将重置以下形式的值:…clearDate(datedata,value)
-这些值来自哪里?您的意思是clearMyInput:(datedata,value)=>{
?值“datedata,value”不应包含在此代码示例中。我很抱歉。基本上,在我提交此redux表单后,表单现在填充了输入中的值。此时,我希望单击按钮,清除输入字段。