Reactjs 在不使用this.state和setState的情况下,如何使用Redux传递输入值
在学习Redux的过程中,我设法使用了提供者、子组件、连接它们等等 但我在子组件中有表单,我想在提交时将数据从子组件传递给父组件。我不想使用'value onChange setState value'方法。我想用Redux来处理这个问题 我也可以考虑执行onChange分派,但我只想在发生onSubmit时传递值Reactjs 在不使用this.state和setState的情况下,如何使用Redux传递输入值,reactjs,redux,Reactjs,Redux,在学习Redux的过程中,我设法使用了提供者、子组件、连接它们等等 但我在子组件中有表单,我想在提交时将数据从子组件传递给父组件。我不想使用'value onChange setState value'方法。我想用Redux来处理这个问题 我也可以考虑执行onChange分派,但我只想在发生onSubmit时传递值 import React from "react"; class InputComponent extends React.Component { addNewTodo = (
import React from "react";
class InputComponent extends React.Component {
addNewTodo = (e) => {
e.preventDefault()
console.log(e.target);
};
render() {
return (
<form onSubmit={this.addNewTodo}>
<input
type="text"
class="form-control form-control-lg addTodoform"
id='addTodoform'
placeholder="Add new task or note"
defaultValue=''
/>
<button type="submit" class='btn btn-primary' >
Add
</button>
</form>
);
}
}
export default InputComponent;
从“React”导入React;
类InputComponent扩展了React.Component{
addNewTodo=(e)=>{
e、 预防默认值()
console.log(如target);
};
render(){
返回(
添加
);
}
}
导出默认输入组件;
实现所需功能的一种方法是使用文档
API访问表单输入的值addTodoform
,如下所示:
const inputValue = document.getElementById('addTodoform').value;
因此,在addNewTodo()
处理程序中,您可以执行以下操作:
class InputComponent extends React.Component {
addNewTodo = (e) => {
e.preventDefault()
console.log(e.target);
// Get value from input directly, without the use of setState, etc
const inputValue = document.getElementById('addTodoform').value;
// Dispatch inputValue to your redux action
// dispatch(submitAction(inputValue))
};
render() {
return (
<form onSubmit={this.addNewTodo}>
<input
type="text"
class="form-control form-control-lg addTodoform"
id='addTodoform'
placeholder="Add new task or note"
defaultValue=''
/>
<button type="submit" class='btn btn-primary' >
Add
</button>
</form>
);
}
}
类InputComponent扩展了React.Component{
addNewTodo=(e)=>{
e、 预防默认值()
console.log(如target);
//直接从输入中获取值,无需使用setState等
const inputValue=document.getElementById('addTodoform')。值;
//将inputValue分派到您的redux操作
//分派(提交(输入值))
};
render(){
返回(
添加
);
}
}
希望这对您有所帮助您是否定义了redux reducer,以及提交操作?我想先通过控制台记录输入值,然后将其与dispatch和reducer一起传递到存储区。我可以做动作减速器部分,但是我如何在没有设置状态的情况下传递输入值?