A basic form with React

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have been working of a basic from using React and I need your help to improve this part of my code, especially validate and onSubmit.



state = 
email: "",
emailError: "",
;
change = e =>
this.setState(
[e.target.name]: e.target.value
);
;

validate = () =>
let isError = false;
const errors =
emailError: "",
;

if (this.state.email.indexOf("@") === -1)
isError = true;
errors.emailError = "Requires valid email";


this.setState(
...this.state,
...errors
);

return isError;
;
onSubmit = e =>
e.preventDefault();
const err = this.validate();
if (!err)
this.props.onSubmit(this.state);
this.setState(

email: "",
emailError: "",
);

;

render() {
return (
<form>
<TextField
name="email"
hintText="Email"
floatingLabelText="Email"
value=this.state.email
onChange=e => this.change(e)
errorText=this.state.emailError
floatingLabelFixed
/>
<br />
<RaisedButton label="Submit" onClick=e => this.onSubmit(e) primary />
</form>
);






share|improve this question



























    up vote
    2
    down vote

    favorite












    I have been working of a basic from using React and I need your help to improve this part of my code, especially validate and onSubmit.



    state = 
    email: "",
    emailError: "",
    ;
    change = e =>
    this.setState(
    [e.target.name]: e.target.value
    );
    ;

    validate = () =>
    let isError = false;
    const errors =
    emailError: "",
    ;

    if (this.state.email.indexOf("@") === -1)
    isError = true;
    errors.emailError = "Requires valid email";


    this.setState(
    ...this.state,
    ...errors
    );

    return isError;
    ;
    onSubmit = e =>
    e.preventDefault();
    const err = this.validate();
    if (!err)
    this.props.onSubmit(this.state);
    this.setState(

    email: "",
    emailError: "",
    );

    ;

    render() {
    return (
    <form>
    <TextField
    name="email"
    hintText="Email"
    floatingLabelText="Email"
    value=this.state.email
    onChange=e => this.change(e)
    errorText=this.state.emailError
    floatingLabelFixed
    />
    <br />
    <RaisedButton label="Submit" onClick=e => this.onSubmit(e) primary />
    </form>
    );






    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have been working of a basic from using React and I need your help to improve this part of my code, especially validate and onSubmit.



      state = 
      email: "",
      emailError: "",
      ;
      change = e =>
      this.setState(
      [e.target.name]: e.target.value
      );
      ;

      validate = () =>
      let isError = false;
      const errors =
      emailError: "",
      ;

      if (this.state.email.indexOf("@") === -1)
      isError = true;
      errors.emailError = "Requires valid email";


      this.setState(
      ...this.state,
      ...errors
      );

      return isError;
      ;
      onSubmit = e =>
      e.preventDefault();
      const err = this.validate();
      if (!err)
      this.props.onSubmit(this.state);
      this.setState(

      email: "",
      emailError: "",
      );

      ;

      render() {
      return (
      <form>
      <TextField
      name="email"
      hintText="Email"
      floatingLabelText="Email"
      value=this.state.email
      onChange=e => this.change(e)
      errorText=this.state.emailError
      floatingLabelFixed
      />
      <br />
      <RaisedButton label="Submit" onClick=e => this.onSubmit(e) primary />
      </form>
      );






      share|improve this question













      I have been working of a basic from using React and I need your help to improve this part of my code, especially validate and onSubmit.



      state = 
      email: "",
      emailError: "",
      ;
      change = e =>
      this.setState(
      [e.target.name]: e.target.value
      );
      ;

      validate = () =>
      let isError = false;
      const errors =
      emailError: "",
      ;

      if (this.state.email.indexOf("@") === -1)
      isError = true;
      errors.emailError = "Requires valid email";


      this.setState(
      ...this.state,
      ...errors
      );

      return isError;
      ;
      onSubmit = e =>
      e.preventDefault();
      const err = this.validate();
      if (!err)
      this.props.onSubmit(this.state);
      this.setState(

      email: "",
      emailError: "",
      );

      ;

      render() {
      return (
      <form>
      <TextField
      name="email"
      hintText="Email"
      floatingLabelText="Email"
      value=this.state.email
      onChange=e => this.change(e)
      errorText=this.state.emailError
      floatingLabelFixed
      />
      <br />
      <RaisedButton label="Submit" onClick=e => this.onSubmit(e) primary />
      </form>
      );








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 10 at 16:07









      200_success

      123k14142399




      123k14142399









      asked Mar 23 at 9:40









      DDD

      112




      112




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          As far as I'm aware, CodeReview is for completed programs. I imagine that this question would be better suited in StackOverflow. That being said, I see no point in duping the question on a different site.



          Without seeing the rest of the program, and solely in a time interest, I'd say you keep the change function and ignore the validations. If you want a form field to be required, you can add 'required' to the input.



          <form>
          <input type="text" name="input1" value=this.state.input1 onChange=e => this.change(e) required />
          </form>


          You can find more on the MDN web docs for the required attribute.



          Side note: you can also bind this.change as this.change = this.change.bind(this) in the constructor if you intend on using it for multiple fields. It'll save you some bytes, but if it's just a one off, binding it in the element itself like you have should be fine.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190288%2fa-basic-form-with-react%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            As far as I'm aware, CodeReview is for completed programs. I imagine that this question would be better suited in StackOverflow. That being said, I see no point in duping the question on a different site.



            Without seeing the rest of the program, and solely in a time interest, I'd say you keep the change function and ignore the validations. If you want a form field to be required, you can add 'required' to the input.



            <form>
            <input type="text" name="input1" value=this.state.input1 onChange=e => this.change(e) required />
            </form>


            You can find more on the MDN web docs for the required attribute.



            Side note: you can also bind this.change as this.change = this.change.bind(this) in the constructor if you intend on using it for multiple fields. It'll save you some bytes, but if it's just a one off, binding it in the element itself like you have should be fine.






            share|improve this answer

























              up vote
              2
              down vote













              As far as I'm aware, CodeReview is for completed programs. I imagine that this question would be better suited in StackOverflow. That being said, I see no point in duping the question on a different site.



              Without seeing the rest of the program, and solely in a time interest, I'd say you keep the change function and ignore the validations. If you want a form field to be required, you can add 'required' to the input.



              <form>
              <input type="text" name="input1" value=this.state.input1 onChange=e => this.change(e) required />
              </form>


              You can find more on the MDN web docs for the required attribute.



              Side note: you can also bind this.change as this.change = this.change.bind(this) in the constructor if you intend on using it for multiple fields. It'll save you some bytes, but if it's just a one off, binding it in the element itself like you have should be fine.






              share|improve this answer























                up vote
                2
                down vote










                up vote
                2
                down vote









                As far as I'm aware, CodeReview is for completed programs. I imagine that this question would be better suited in StackOverflow. That being said, I see no point in duping the question on a different site.



                Without seeing the rest of the program, and solely in a time interest, I'd say you keep the change function and ignore the validations. If you want a form field to be required, you can add 'required' to the input.



                <form>
                <input type="text" name="input1" value=this.state.input1 onChange=e => this.change(e) required />
                </form>


                You can find more on the MDN web docs for the required attribute.



                Side note: you can also bind this.change as this.change = this.change.bind(this) in the constructor if you intend on using it for multiple fields. It'll save you some bytes, but if it's just a one off, binding it in the element itself like you have should be fine.






                share|improve this answer













                As far as I'm aware, CodeReview is for completed programs. I imagine that this question would be better suited in StackOverflow. That being said, I see no point in duping the question on a different site.



                Without seeing the rest of the program, and solely in a time interest, I'd say you keep the change function and ignore the validations. If you want a form field to be required, you can add 'required' to the input.



                <form>
                <input type="text" name="input1" value=this.state.input1 onChange=e => this.change(e) required />
                </form>


                You can find more on the MDN web docs for the required attribute.



                Side note: you can also bind this.change as this.change = this.change.bind(this) in the constructor if you intend on using it for multiple fields. It'll save you some bytes, but if it's just a one off, binding it in the element itself like you have should be fine.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Apr 10 at 14:21









                Chimera.Zen

                577




                577






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190288%2fa-basic-form-with-react%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation