【问题标题】:Updating the state on every post request更新每个发布请求的状态
【发布时间】:2020-04-23 16:26:23
【问题描述】:

嘿,伙计们,我在我的 react redux 项目中有一个表单,所以在提交表单时,我会调用一个操作,该操作会点击 post api 以将文档插入到 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};

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

【问题讨论】:

    标签: reactjs redux react-redux redux-thunk


    【解决方案1】:

    您需要合并状态中的数据而不是覆盖它。在你的 reducer 中使用扩展语法来实现这一点,如下所示

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

    一旦你在 reducer 中更新了 state,你就可以在组件中使用 connect 和 mapStateToProps 来访问它

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

    【讨论】:

    • 你能告诉我为什么 {...state,tasks:action.payload};不行吗?
    • 那是因为,状态被定义为一个数组,需要保持和数组更新后
    • 只是对刷新页面的最后一个疑问,我调用了我的操作和一个名为 getPomodoro 的新减速器并更新状态,如 case GETDATA: return {...state,tasks:action.payload};现在我想渲染它,但是在我刷新我的应用程序之前,我正在更新 postPomodoro 中的状态,因为你回答并想要显示 postPomodoro 的状态........我该怎么做?我如何决定何时显示哪些减速器状态?任何准则?
    • 现在如果我刷新我想渲染通过我的 getPomodoro 减速器获得的数据,否则从我的 postPomodoro 渲染它
    • 从reducer获取两个数据,如果getPomodoro数据可用则渲染它,否则渲染post数据
    猜你喜欢
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多