简体   繁体   English

使用 connect() 响应 Redux 表单?

[英]React Redux form with connect()?

i am trying to build a react redux form using the connect() instead useSelector and useDispatch.我正在尝试使用 connect() 而不是 useSelector 和 useDispatch 来构建一个 react redux 表单。 I managed to display the list of data and to reset the forms.我设法显示数据列表并重置表单。 But i didn't manage to send data.但我没有设法发送数据。 Here is the code:这是代码:

 Reducer.js const initialState = { tasks: { name: "", age: "", job: "", }, list: [ { id: 0, name: "Maillard", age: 35, job: "soldier", }, ], }; export const toDoReducer = (state = initialState, action) => { switch (action.type) { case "name": return { ...state, tasks: { name: action.payload, }, }; case "age": return { ...state, tasks: { age: action.payload, }, }; case "job": return { ...state, tasks: { job: action.payload, }, }; case "clear": return { ...state, tasks: { name: "", age: "", job: "", }, }; case "add": return { ...state, tasks: { ...state.tasks, id: state.list.length + 1, }, }; default: return { ...state, }; } }; export default toDoReducer;

 import React from "react"; import { connect } from "react-redux"; import { setName, setAge, setJob, clearForm, addForm, } from "../../redux/action"; export const Form = (props) => { return ( <div> Name <input value={props.list.name} onChange={(e) => { props.Name(e.target.value) }} /> Age <input value={props.list.age} onChange={(e) => { props.Age(e.target.value) }} /> Profession <input value={props.list.job} onChange={(e) => { props.Job(e.target.value) }} /> <div style={{ padding: 20 }}> <button onClick={props.Clear}>Reset</button> <button onClick={props.Add}>Envoyer</button> </div> </div> ); }; const mapDispatchToProps = (dispatch) => { return { Name: () => { dispatch({ type: "name", setName, }); }, Age: () => { dispatch({ type: "age", setAge }); }, Job: () => { dispatch({ type: "job", setJob, }); }, Clear: () => { dispatch({ type: "clear", clearForm, }); }, Add: () => { dispatch({)} // My problem comes from the Add <!-- begin snippet: js hide: false console: true babel: false -->

        type: "add",
        addForm
      })
    }
  };
};

const mapStateToProps = (state) => ({
  list: state.tasks,
});

export default connect(mapStateToProps, mapDispatchToProps)(Form);
// export default Form;

My problems comes from in mapDispatchToProps, i don't know what to do for the function Add我的问题来自于 mapDispatchToProps,我不知道如何为函数添加

Your actions should be the ones defining the action type and you should call them in the mapDispatchToProps , not pass them as part of the action.你的动作应该是定义动作type的动作,你应该在mapDispatchToProps调用它们,而不是将它们作为动作的一部分传递。

So your addForm should be something like所以你的addForm应该是这样的

const addForm = () => ({
   type:'add'
});

and in your mapDispatchToProps it should be like在你的mapDispatchToProps它应该是这样的

Add: () => dispatch(addForm()),

But you have the same problem with all your dispatching但是你所有的调度都有同样的问题

for example the Name should be例如Name应该是

action行动

const setName = (payload) => ({
   type:'name',
   payload
});

mapDispatchToProps mapDispatchToProps

Name: (nameValue) => {
  dispatch(setName(nameValue));
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM