简体   繁体   English

ReactJS - 从 redux 和加载表单中获取数据的正确方法

[英]ReactJS - Proper way to fetch data from redux and load form

I have the reducer:我有减速机:

const INITIAL_STATE = {
  campaign_dates: {
    dt_start: '',
    dt_end: '',
  },

I am submiting my form and them send an action to save the data into redux:我正在提交我的表单,他们发送一个操作将数据保存到 redux:

  function onSubmit(data) {
    dispatch(addCampaignDatesAction(data))
  }

So far so good, I'm able to save the data, but how can I load it in my form?到目前为止一切顺利,我能够保存数据,但如何将其加载到我的表单中?

In my component where my form is, I tried something like this:在我的表单所在的组件中,我尝试了这样的事情:

const getCampaignDatesFromState = useSelector(
    state => state.createCampaign.campaign_dates,
  )

const [startDate, setStartDate] = useState(getCampaignDatesFromState.dt_start !== '' ? new Date(getCampaignDatesFromState.dt_start) : '')

const [endDate, setEndDate] = useState(getCampaignDatesFromState !== '' ? new Date(getCampaignDatesFromState.dt_end) : '')

But I'm getting "Invalid time value at format" error (my start and end date inputs is from react-datepicker)但是我收到“格式时无效的时间值”错误(我的开始和结束日期输入来自 react-datepicker)

What is the proper way to load redux data into your form?将 redux 数据加载到表单中的正确方法是什么?

İf you want to call dispatch and update your store you sould add this lines on your form ;如果你想打电话给 dispatch 并更新你的商店,你应该在你的表格上添加这行;

Firstly added bindActionCreators and connect;首先添加bindActionCreators和connect;

import { connect } from "react-redux";

import { bindActionCreators } from "redux";

and then use connect method for mapStateToProps, mapDispatchToProps;然后对mapStateToProps、mapDispatchToProps使用connect方法;

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(YourComponent);

now you sould define mapStateToProps, mapDispatchToProps like this;现在你应该像这样定义 mapStateToProps、mapDispatchToProps;

const mapStateToProps = state => ({
  getCmpDates: state.campaign_dates
});

const mapDispatchToProps = dispatch => ({
  setCmpDates: bindActionCreators(campaign_dates, dispatch),
});

and now you can change your store with setCmpDates or you can read dates with getCmpDates.现在您可以使用 setCmpDates 更改您的商店,或者您可以使用 getCmpDates 读取日期。

You need to use the connect() method from the react-redux library.您需要使用react-redux库中的connect()方法。 This does also contain some hooks, but you are better off avoiding them, as they can create some strange problems and you loose all the performance benefits of connect .这也包含一些钩子,但最好避免它们,因为它们会产生一些奇怪的问题,并且会失去connect所有性能优势。

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

相关问题 从另一个组件获取的正确方法 - ReactJS - Proper way to fetch from another component - ReactJS ReactJs Redux数据获取 - ReactJs Redux Data Fetch 在没有商店的情况下,使用ReactJS和Flux架构从服务器获取数据的正确方法是什么? - What is the proper way to fetch data from server with ReactJS and Flux architecture where no Store presented? ReactJS - 获取数据并发送到另一个组件的正确方法 - ReactJS - proper way to fetch data and send to another component 使用`fetch`或`request`发送多部分/表单数据的正确方法 - Proper way to send multipart/form-data using `fetch` or `request` ReactJS:通过表单获取和更新数据的惯用方式是什么? - ReactJS: What is the idiomatic way to fetch and update data through a form? 将数据提取存储在本地 state redux reactjs - Store data fetch in a local state redux reactjs 异步数据加载后提交HTML表单的正确方法是什么? - What is the proper way to submit HTML form after asynchronous data load? ReactJS + Redux:维护表单状态的正确和正确做法是什么? - ReactJS + Redux: What's the proper and correct practice to maintain state for a form? ReactJS - 列表、详细信息、新建、编辑页面 - 何时使用 Redux 从 API 加载数据? - ReactJS - List, Detail, New, Edit Pages - When to load data form API with Redux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM