更新每个 post 请求的状态

Updating the state on every post request

嘿伙计们,我在我的 React redux 项目中有一个表单,所以在提交表单时我调用了一个点击 post api 的 Action 来将文档插入 mongodb ,所以我也想要就是每次都更新状态,这样一旦我提交表单,我的状态就会更新,然后我将它们呈现在下面(作为对象数组)

Form.js

class Form extends Component {

  constructor(props) {
    super(props);
    this.state = {
      taskName: "",
      taskDescription: "",

    };
  }



  handleSubmit = () => {
    this.props.addTask(this.state);
  };
  render() {
    return (
      <div className="m-4">
        <div className="d-flex justify-content-between align-items-center">
          {" "}
          <h2 className=" ">Pomodoro Tasks</h2>
          <button className="btn btn-primary" onClick={()=>window.location.reload()}>Refresh</button>
        </div>
        <form>
        <div className="form-group ">
          <label>Task Name</label>
          <input
            type="text"
            className="form-control"
            onChange={(e) => this.setState({ taskName: e.target.value })}
          />
        </div>
        <div className="form-group">
          <label>Task Description</label>
          <input
            type="text"
            className="form-control"
            onChange={(e) => this.setState({ taskDescription: e.target.value })}
          />
        </div>


        </form>

        <button type="submit" className="btn btn-primary" onClick={(e) => this.handleSubmit(e)}>
          Submit
        </button>

       ////render here the state
      </div>
    );
  }
}
const mapStateToProps = (state) => {

  return state
};
export default connect(mapStateToProps, { addTask })(Form);

Actions.js

export const addTask = (details) => {
  return async (dispatch ,getState) =>{

    axios.post('/add_task', details)
      .then(function (response) {
        Swal.fire('Tasks Added !')
        dispatch({type:POSTDATA,payload:response.data})
      })
      .catch(function (error) {
        Swal.fire('Error in adding to database')
        console.log(error);
      });

  };
};

export const getTask = () => {
  return async (dispatch ,getState) =>{
    axios.get('/get_task')
      .then(function (response) {
        dispatch({type:GETDATA,payload:response.data})
      })
      .catch(function (error) {
        console.log(error);
      });
  };
};

reducer.js

const postPomodoro = (state = [], action) => {
  switch (action.type) {
    case POSTDATA:
      return action.payload; //well this is only returning one object that is inserted at that time i need to 
somehow get all the previous data too that are inserted in order to update state with all data and then render it.

    default:
      return state;
  }
};
const getPomodoro = (state = [], action) => {
  switch (action.type) {

    case GETDATA:
      return {...state,tasks:action.payload};
    default:
      return state;
  }
};

我在像

这样的reducer中尝试过
 case POSTDATA:
      return {...state,task:action.payload};

但这也让我返回了最近插入的数据,而不是之前插入的所有数据

您需要合并状态中的数据而不是覆盖它。在你的减速器中使用传播语法来实现如下

const postPomodoro = (state = [], action) => {
  switch (action.type) {
    case POSTDATA:
      return [...state, action.payload]; 

    default:
      return state;
  }
};

更新 reducer 中的状态后,您可以使用 connect 和 mapStateToProps 在组件中访问它

const Comp = (props) => {
   // iterate over props.postTask and render
}
const mapStateProps = (state) => {
   return {
      postTask: state.postPomodoro
   }
}
export default connect(mapStateToProps)(Comp)