11

I am kind of stumped on this simple issue! I want to simply take my form data, validate it, submit it and submit a post request to the Express API. But before that, I don't think I have a thorough understanding of how to accomplish this. I looked at this question and these and bunch of others but I am not sure this is the best approach.

This is how I assume it will be undertaken:

I create a React Component for the sign up page. (Simplified for demonstration)

class SignupForm extends Component {
    constructor(props){
        super(props);
        this.onSubmit = this.onSubmit.bind(this);
    }
    onSubmit(val){
        debugger;
    }
    render(){
        return (
            <form onSUbmit={ (e)=> this.onSubmit(e) }>
                <input type="text" />
                <label></label>
                <button type="submit">Submit</button>
            </form>
        )
    }
}

When button is clicked the OnSubmit() function triggers, where it will be passed JSON data.

{
   "name": "Kanye",
   "surname": "West",
   "email":"[email protected]",
   "password":"notsurehowthiswillwork"
}

Where I can trigger my action SignupAction() which will make an AJAX call to my API and then update my reducers.

The confusion multiplies when it comes to using libraries like react-redux-form or redux-form. I just want to simply have a model or something for name, surname email and password, but I feel that these libraries are over-engineered as soon as they start having their own reducer like:

const store = createStore(combineForms({
   user: initialUser,
}));

MY OTHER OPTION IS:

class SignupForm extends Component {

    constructor(props){
        super(props);
        this.state.form = {
            name : '',
            surname: '',
            email: '',
            password: ''
        }
    }
    onSubmit(e){
        e.preventDefault();
        this.props.SignupAction(this.state.form);
        // then reset the state agian to '' 
    }
}

But, my question is... will this effect performance and if so WHY?

1
  • 1
    It won't affect on your performance. In both cases component will re render the same number of times. I believe people uses redux-form because of other benefits like validation, keeping everything in one place, etc.
    – niba
    Oct 3, 2016 at 21:33

2 Answers 2

8

Very easy way to deal with forms:

class UserInfo extends React.Component {

  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleSubmit(e) {
    e.preventDefault();
    
    const formData = {};
    for (const field in this.refs) {
      formData[field] = this.refs[field].value;
    }
    console.log('-->', formData);
  }

  render() {
    return (
        <div>
          <form onSubmit={this.handleSubmit}>
            <input ref="phone" className="phone" type='tel' name="phone"/>
            <input ref="email" className="email" type='tel' name="email"/>
            <input type="submit" value="Submit"/>
          </form>
        </div>
    );
  }
}

export default UserInfo;

1
  • 9
    you should make your inputs controlled and not use refs
    – nbkhope
    Feb 15, 2017 at 1:47
5

I would suggest using redux-form. It gives you following options:

  1. Input validation
  2. Different types on inputs including date and file uploads
  3. Provides a onSubmit method which is called after your validation succeeds (this is point where you dispatch action to call your API and update state)

But if still dont want to use (I would strongly recommend using it), what you can do is on form submit just validate your data and dispatch an action in your container. So pass your data as parameters from your component to your container where you dispatch an action calls post/put API (in redux form you dont need to pass anything, you can directly read from the store).

    onSubmit(val){
        debugger;
    }
    render(){
        const { onSubmit } = this.props //pass onSubmit from 
        return (
            <form onSubmit={ (e)=> {onSubmit} ) }>
                <input type="text" />
                <label></label>
                <button type="submit">Submit</button>
            </form>
        )
    }
}

Container:

mapDispatchToProps(dispatch){
   return {
    onSubmit => {
     //dispatch action
     }
  }
2
  • 1
    I actually decided to use react-redux-form! The creator personally helped me and it cleared up a lot of things! @davidkpiano is amazing!
    – dsomel21
    Oct 4, 2016 at 16:38
  • react-redux-form is way better imo, you should try it out Apr 11, 2018 at 19:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.