Reading form inputs?









up vote
-1
down vote

favorite












How would you read the input value ?



On the reacjs site I see very complicated way !!



https://reactjs.org/docs/forms.html



I just want to read the value and submit it via ajax fetch() request. I.e. I don't need to manage bindings, events and such ...










share|improve this question





















  • Event handlers detect when a user is interacting with our app, by clicking or dragging or whatever. This is the absolute goal of the React library, responding to user interactions.
    – Daniel
    Nov 16 at 4:55














up vote
-1
down vote

favorite












How would you read the input value ?



On the reacjs site I see very complicated way !!



https://reactjs.org/docs/forms.html



I just want to read the value and submit it via ajax fetch() request. I.e. I don't need to manage bindings, events and such ...










share|improve this question





















  • Event handlers detect when a user is interacting with our app, by clicking or dragging or whatever. This is the absolute goal of the React library, responding to user interactions.
    – Daniel
    Nov 16 at 4:55












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











How would you read the input value ?



On the reacjs site I see very complicated way !!



https://reactjs.org/docs/forms.html



I just want to read the value and submit it via ajax fetch() request. I.e. I don't need to manage bindings, events and such ...










share|improve this question













How would you read the input value ?



On the reacjs site I see very complicated way !!



https://reactjs.org/docs/forms.html



I just want to read the value and submit it via ajax fetch() request. I.e. I don't need to manage bindings, events and such ...







ajax reactjs forms fetch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 3:27









user1019129

1,89622326




1,89622326











  • Event handlers detect when a user is interacting with our app, by clicking or dragging or whatever. This is the absolute goal of the React library, responding to user interactions.
    – Daniel
    Nov 16 at 4:55
















  • Event handlers detect when a user is interacting with our app, by clicking or dragging or whatever. This is the absolute goal of the React library, responding to user interactions.
    – Daniel
    Nov 16 at 4:55















Event handlers detect when a user is interacting with our app, by clicking or dragging or whatever. This is the absolute goal of the React library, responding to user interactions.
– Daniel
Nov 16 at 4:55




Event handlers detect when a user is interacting with our app, by clicking or dragging or whatever. This is the absolute goal of the React library, responding to user interactions.
– Daniel
Nov 16 at 4:55












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










The easiest way by far to read values from html controls is by using an event handler.



export default class myComponent extends Component 
person = ;

onChange = field => e =>
this.person[field] = e.target.value;
;

render()
return (
<Input
id="firstName"
name="firstName"
autoComplete="firstName"
autoFocus
onChange=this.onChange('FirstName')
/>
);




In the above code snippet we are basically telling react to fire the onChange member on an update of firstName control update. Our method will receive an event e, that has a handle to our control and we can basically probe it's value member to get what's typed in (much like jquery's $('#element').value()).



Why is it the easiest method? because it's generic enough to allow you to handle multiple inputs in a react component. Notice that, I'm also instructing React to pass me the control name in addition to the event, and using this method I can basically know exactly which of my inputs (in case of multiple) caused the event to fire.






share|improve this answer




















  • why not just something akin to getElementbyID()
    – user1019129
    Nov 13 at 0:30






  • 1




    I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
    – Mpho Shaun Majenge
    Nov 13 at 1:02

















up vote
1
down vote













Reading user input value is feasible and recommended via event handlers.



Below example would explain how to read input value and send it to the backend via fetch when Form is submitted



 class Test extends Component
constructor(props)
super(props);
this.state =
name: “”



handleChange = event =>
this.setState(name: event.target.value);


handleSubmit = () =>
//send the value via fetch backend I.e., this.state.name

render()
const name = this.state;
render(
<form onSubmit=this.handleSubmit
<label>
Name:
<input type="text" value=name onChange=this.handleChange name="name" />
</label>
<input type="submit" value="Submit" />
</form>
)







share|improve this answer




















    Your Answer






    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: "1"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255591%2freading-form-inputs%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The easiest way by far to read values from html controls is by using an event handler.



    export default class myComponent extends Component 
    person = ;

    onChange = field => e =>
    this.person[field] = e.target.value;
    ;

    render()
    return (
    <Input
    id="firstName"
    name="firstName"
    autoComplete="firstName"
    autoFocus
    onChange=this.onChange('FirstName')
    />
    );




    In the above code snippet we are basically telling react to fire the onChange member on an update of firstName control update. Our method will receive an event e, that has a handle to our control and we can basically probe it's value member to get what's typed in (much like jquery's $('#element').value()).



    Why is it the easiest method? because it's generic enough to allow you to handle multiple inputs in a react component. Notice that, I'm also instructing React to pass me the control name in addition to the event, and using this method I can basically know exactly which of my inputs (in case of multiple) caused the event to fire.






    share|improve this answer




















    • why not just something akin to getElementbyID()
      – user1019129
      Nov 13 at 0:30






    • 1




      I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
      – Mpho Shaun Majenge
      Nov 13 at 1:02














    up vote
    1
    down vote



    accepted










    The easiest way by far to read values from html controls is by using an event handler.



    export default class myComponent extends Component 
    person = ;

    onChange = field => e =>
    this.person[field] = e.target.value;
    ;

    render()
    return (
    <Input
    id="firstName"
    name="firstName"
    autoComplete="firstName"
    autoFocus
    onChange=this.onChange('FirstName')
    />
    );




    In the above code snippet we are basically telling react to fire the onChange member on an update of firstName control update. Our method will receive an event e, that has a handle to our control and we can basically probe it's value member to get what's typed in (much like jquery's $('#element').value()).



    Why is it the easiest method? because it's generic enough to allow you to handle multiple inputs in a react component. Notice that, I'm also instructing React to pass me the control name in addition to the event, and using this method I can basically know exactly which of my inputs (in case of multiple) caused the event to fire.






    share|improve this answer




















    • why not just something akin to getElementbyID()
      – user1019129
      Nov 13 at 0:30






    • 1




      I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
      – Mpho Shaun Majenge
      Nov 13 at 1:02












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    The easiest way by far to read values from html controls is by using an event handler.



    export default class myComponent extends Component 
    person = ;

    onChange = field => e =>
    this.person[field] = e.target.value;
    ;

    render()
    return (
    <Input
    id="firstName"
    name="firstName"
    autoComplete="firstName"
    autoFocus
    onChange=this.onChange('FirstName')
    />
    );




    In the above code snippet we are basically telling react to fire the onChange member on an update of firstName control update. Our method will receive an event e, that has a handle to our control and we can basically probe it's value member to get what's typed in (much like jquery's $('#element').value()).



    Why is it the easiest method? because it's generic enough to allow you to handle multiple inputs in a react component. Notice that, I'm also instructing React to pass me the control name in addition to the event, and using this method I can basically know exactly which of my inputs (in case of multiple) caused the event to fire.






    share|improve this answer












    The easiest way by far to read values from html controls is by using an event handler.



    export default class myComponent extends Component 
    person = ;

    onChange = field => e =>
    this.person[field] = e.target.value;
    ;

    render()
    return (
    <Input
    id="firstName"
    name="firstName"
    autoComplete="firstName"
    autoFocus
    onChange=this.onChange('FirstName')
    />
    );




    In the above code snippet we are basically telling react to fire the onChange member on an update of firstName control update. Our method will receive an event e, that has a handle to our control and we can basically probe it's value member to get what's typed in (much like jquery's $('#element').value()).



    Why is it the easiest method? because it's generic enough to allow you to handle multiple inputs in a react component. Notice that, I'm also instructing React to pass me the control name in addition to the event, and using this method I can basically know exactly which of my inputs (in case of multiple) caused the event to fire.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 3:42









    Mpho Shaun Majenge

    1416




    1416











    • why not just something akin to getElementbyID()
      – user1019129
      Nov 13 at 0:30






    • 1




      I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
      – Mpho Shaun Majenge
      Nov 13 at 1:02
















    • why not just something akin to getElementbyID()
      – user1019129
      Nov 13 at 0:30






    • 1




      I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
      – Mpho Shaun Majenge
      Nov 13 at 1:02















    why not just something akin to getElementbyID()
    – user1019129
    Nov 13 at 0:30




    why not just something akin to getElementbyID()
    – user1019129
    Nov 13 at 0:30




    1




    1




    I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
    – Mpho Shaun Majenge
    Nov 13 at 1:02




    I suppose you can still use the classical document.getElementById but you'd be loosing out so much than using the already provided reactjs mechanism. I think the react way is better. [quick note] since react uses a VDOM, I'd just hope that by the time of running the document.getElementById (typically inside componentDidMount()) the elements are in the Virtual. I think it's just too much work
    – Mpho Shaun Majenge
    Nov 13 at 1:02












    up vote
    1
    down vote













    Reading user input value is feasible and recommended via event handlers.



    Below example would explain how to read input value and send it to the backend via fetch when Form is submitted



     class Test extends Component
    constructor(props)
    super(props);
    this.state =
    name: “”



    handleChange = event =>
    this.setState(name: event.target.value);


    handleSubmit = () =>
    //send the value via fetch backend I.e., this.state.name

    render()
    const name = this.state;
    render(
    <form onSubmit=this.handleSubmit
    <label>
    Name:
    <input type="text" value=name onChange=this.handleChange name="name" />
    </label>
    <input type="submit" value="Submit" />
    </form>
    )







    share|improve this answer
























      up vote
      1
      down vote













      Reading user input value is feasible and recommended via event handlers.



      Below example would explain how to read input value and send it to the backend via fetch when Form is submitted



       class Test extends Component
      constructor(props)
      super(props);
      this.state =
      name: “”



      handleChange = event =>
      this.setState(name: event.target.value);


      handleSubmit = () =>
      //send the value via fetch backend I.e., this.state.name

      render()
      const name = this.state;
      render(
      <form onSubmit=this.handleSubmit
      <label>
      Name:
      <input type="text" value=name onChange=this.handleChange name="name" />
      </label>
      <input type="submit" value="Submit" />
      </form>
      )







      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Reading user input value is feasible and recommended via event handlers.



        Below example would explain how to read input value and send it to the backend via fetch when Form is submitted



         class Test extends Component
        constructor(props)
        super(props);
        this.state =
        name: “”



        handleChange = event =>
        this.setState(name: event.target.value);


        handleSubmit = () =>
        //send the value via fetch backend I.e., this.state.name

        render()
        const name = this.state;
        render(
        <form onSubmit=this.handleSubmit
        <label>
        Name:
        <input type="text" value=name onChange=this.handleChange name="name" />
        </label>
        <input type="submit" value="Submit" />
        </form>
        )







        share|improve this answer












        Reading user input value is feasible and recommended via event handlers.



        Below example would explain how to read input value and send it to the backend via fetch when Form is submitted



         class Test extends Component
        constructor(props)
        super(props);
        this.state =
        name: “”



        handleChange = event =>
        this.setState(name: event.target.value);


        handleSubmit = () =>
        //send the value via fetch backend I.e., this.state.name

        render()
        const name = this.state;
        render(
        <form onSubmit=this.handleSubmit
        <label>
        Name:
        <input type="text" value=name onChange=this.handleChange name="name" />
        </label>
        <input type="submit" value="Submit" />
        </form>
        )








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 4:27









        Hemadri Dasari

        7,02611039




        7,02611039



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255591%2freading-form-inputs%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党