2

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:

  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)

What is the proper way to load redux data into your form?

7
  • Have you check mapStateToProps? Commented Dec 30, 2019 at 12:55
  • I'm using react hooks, tried with useEffect but same error pops up Commented Dec 30, 2019 at 13:07
  • @ShoebMirza he is using the new hooks style useSelector Commented Dec 30, 2019 at 13:08
  • 1
    The problem looks like time format related, but not react-redux. console.log(getCampaignDatesFromState) and check it's format. Commented Dec 30, 2019 at 13:10
  • 1
    I got it, I was passing an object, when I just separate them with const startCampaignDate = useSelector( state => state.createCampaign.campaign_dates.dt_start, ) its working now. Thanks @SuleymanSah Commented Dec 30, 2019 at 14:13

2 Answers 2

2

İf you want to call dispatch and update your store you sould add this lines on your form ;

Firstly added bindActionCreators and connect;

import { connect } from "react-redux";

import { bindActionCreators } from "redux";

and then use connect method for mapStateToProps, mapDispatchToProps;

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

now you sould define mapStateToProps, mapDispatchToProps like this;

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use the connect() method from the react-redux library. 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.

1 Comment

@fjurr you don't need to use connect. There is nothing wrong with the new hooks style react-redux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.