0

I'm learning to use react-redux, so sorry if I'm doing confusing!

I'm trying to create a form and insert data into the db.

Then I have created 2 page, one for the form and another one.

RequestForm

handleChange = (e) => {
    let meeting = this.state.meeting;
    meeting[e.target.name] = e.target.value;
    this.setState({ meeting });
  };

  handleSubmit(event) {
    event.preventDefault();
    console.log(this.state.meeting);
    this.props.addMeeting(this.state)
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>

        <div>
          <label>Motivation:</label>
          <input
            type="text"
            name="motivation"
            onChange={(event) => this.handleChange(event)}
          />
        </div>
        <div>
          <label>Date:</label>
          <input
            type="date"
            name="date"
            onChange={(event) => this.handleChange(event)}
          />
        </div>

MeetingRequest.js

import RequestForm from './RequestForm';
import { connect } from 'react-redux';
import {addMeeting} from '../Redux/actions';


class MeetingRequest extends Component {

  render() {
    const addMeeting = this.props
    return (
      <div>
        <h2>Request Meeting</h2>
        <RequestForm 
          addMeeting={addMeeting}
        />
      </div>
    );
  }
}

function mapDispatchToProps(dispatch) {
  return {
    addMeeting: (meeting) => dispatch(addMeeting(meeting)),
  };
}

export default connect(null, mapDispatchToProps)(MeetingRequest);

EDIT. Thank you to the comments the firt error is resolved, now I have a 500 problem. Could it be a problem about the Actions.js??

And also in your opinion is right how I'm trying to do??

Thank you

5
  • 1
    try const { addMeeting } = this.props in MeetingRequest.js Commented Apr 9, 2020 at 15:46
  • @MaximMazurok Now it gives me another problem about the api, but in your opionion is right how I'm operating? About action and reducers? Commented Apr 9, 2020 at 15:48
  • Because now give me a 500 error, so the problem is about the api Commented Apr 9, 2020 at 15:51
  • Yes, I think so Commented Apr 9, 2020 at 15:55
  • I've added answer, please, upvote/accept if it helps. Regarding 500, ask new question as it doesn't seem to be related to original question :) Commented Apr 9, 2020 at 16:02

1 Answer 1

1

Change:

-const addMeeting = this.props
+const { addMeeting } = this.props

in MeetingRequest.js


Regarding your 500 error on API, it may be because you're sending JSON while setting multipart/form-data. Inspect using Network tab in dev tools. If your backend need multipart/form-data you have to convert your state.meeting object to FormData

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

3 Comments

Thank you so much for your help. Maybe the error could be in addMeeting: (meeting) => dispatch(addMeeting(meeting)), , in mapDispatchToProps ?? Anyway How can I do to convert in formdata?
@Jack23 to assist you with that I'll have to take a look at your action and reducer that actually makes request. General idea: before making request, create new FormData object and fill it with data from your meeting object, and set FormData instead of your plain meeting object.
Hi thank you for hour help. I have created a new request, if you can give me an hand, stackoverflow.com/questions/61136375/…

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.