Reading form inputs?
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
add a comment |
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
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 '18 at 4:55
add a comment |
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
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
ajax reactjs forms fetch
asked Nov 12 '18 at 3:27
stensten
1,94122326
1,94122326
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 '18 at 4:55
add a comment |
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 '18 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 '18 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 '18 at 4:55
add a comment |
2 Answers
2
active
oldest
votes
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.
why not just something akin to getElementbyID()
– sten
Nov 13 '18 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 '18 at 1:02
add a comment |
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>
)
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
why not just something akin to getElementbyID()
– sten
Nov 13 '18 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 '18 at 1:02
add a comment |
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.
why not just something akin to getElementbyID()
– sten
Nov 13 '18 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 '18 at 1:02
add a comment |
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.
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.
answered Nov 12 '18 at 3:42
Mpho Shaun MajengeMpho Shaun Majenge
1416
1416
why not just something akin to getElementbyID()
– sten
Nov 13 '18 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 '18 at 1:02
add a comment |
why not just something akin to getElementbyID()
– sten
Nov 13 '18 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 '18 at 1:02
why not just something akin to getElementbyID()
– sten
Nov 13 '18 at 0:30
why not just something akin to getElementbyID()
– sten
Nov 13 '18 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 '18 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 '18 at 1:02
add a comment |
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>
)
add a comment |
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>
)
add a comment |
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>
)
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>
)
answered Nov 12 '18 at 4:27
Hemadri DasariHemadri Dasari
7,58411339
7,58411339
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 '18 at 4:55