How I can push my state elements to the state array?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Why aren't rockets built with truss structures inside their fuel & oxidizer tanks to increase structural strength?
Telephone number in spoken words
Least cost swapping in C++
What are the odds of rolling specific ability score totals in D&D?
Is there a word for returning to unpreparedness?
Weird resistor with dots around it
"Mouth-breathing" as slang for stupidity
Is there any way I will not use l'Hôpital's rule here?
What modifiers are added to the attack and damage rolls of this unique longbow from Waterdeep: Dragon Heist?
Stop email from sending using AMPscript
Are there liquid fueled rocket boosters having coaxial fuel/oxidizer tanks?
Is the Microsoft recommendation to use C# properties applicable to game development?
Why does Japan use the same type of AC power outlet as the US?
How to not forget things?
What is the opposite of "hunger level"?
Why does this Jet Provost strikemaster have a textured leading edge?
Is Thieves' Cant a language?
Scam? Phone call from "Department of Social Security" asking me to call back
Lípínguapua dopo Pêpê
A man in the desert is bitten by a skeletal animal, its skull gets stuck on his arm
Solving pricing problem heuristically in column generation algorithm for VRP
How do I ask for 2-3 days per week remote work in a job interview?
Can anybody tell me who this Pokemon is?
Illustrator - SVG make thinner path
How I can push my state elements to the state array?
How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?How do I redirect to another webpage?How to check whether a string contains a substring in JavaScript?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm currently trying to push my state elements (user_number_X) into an array (this.state.results.numbers) but my input handler function is not working.
How can I rewrite it so it will push input values into an array?
Here is my code:
export default class InputArea extends React.Component
constructor(props)
super(props);
this.state =
draw_number: '',
user_number_1: '',
user_number_2: '',
user_number_3: '',
user_number_4: '',
user_number_5: '',
user_number_6: '',
results :
numbers: [],
draws: ''
/* handledrawsChange = event =>
this.setState(
event
)
*/
inputHandler = event =>
this.setState([event.target.name]: event.target.value);
this.state.results.numbers.push(event.target.value)
// this.state.results.numbers.push(event.target.value)//this.setState(number: this.state.results.numbers.push(parseInt(event.target.value)));
componentDidMount()
fetch("http://localhost:8080/simulate",
method: "POST",
body: this.state.results
);
render()
console.log(this.state);
return (
<InputField>
<InputWrapper>
<Label>1st number</Label>
<Input name="user_number_1" type="number" maxlength="2" value=this.state.user_number_1 onChange=this.inputHandler required></Input>
<Label>2nd number</Label>
<Input name="user_number_2" type="number" maxlength="2" value=this.state.user_number_2 onChange=this.inputHandler required></Input>
<Label>3rd number</Label>
<Input name="user_number_3" type="number" maxlength="2" value=this.state.user_number_3 onChange=this.inputHandler required></Input>
<Label>4th number</Label>
<Input name="user_number_4" type="number" maxlength="2" value=this.state.user_number_4 onChange=this.inputHandler required></Input>
<Label>5th number</Label>
<Input name="user_number_5" type="number" maxlength="2" value=this.state.user_number_5 onChange=this.inputHandler required></Input>
<Label>6th number</Label>
<Input name="user_number_6" type="number" maxlength="2" value=this.state.user_number_6 onChange=this.inputHandler required></Input>
<Label>Number of draws:</Label>
<Input name="draw_number" type="number" value=this.state.draw_number onChange=this.inputHandler required></Input>
</InputWrapper>
<Button type="submit" /*onClick=this.drawsHandler*/>Let's win!</Button>
</InputField>
)
javascript reactjs
add a comment |
I'm currently trying to push my state elements (user_number_X) into an array (this.state.results.numbers) but my input handler function is not working.
How can I rewrite it so it will push input values into an array?
Here is my code:
export default class InputArea extends React.Component
constructor(props)
super(props);
this.state =
draw_number: '',
user_number_1: '',
user_number_2: '',
user_number_3: '',
user_number_4: '',
user_number_5: '',
user_number_6: '',
results :
numbers: [],
draws: ''
/* handledrawsChange = event =>
this.setState(
event
)
*/
inputHandler = event =>
this.setState([event.target.name]: event.target.value);
this.state.results.numbers.push(event.target.value)
// this.state.results.numbers.push(event.target.value)//this.setState(number: this.state.results.numbers.push(parseInt(event.target.value)));
componentDidMount()
fetch("http://localhost:8080/simulate",
method: "POST",
body: this.state.results
);
render()
console.log(this.state);
return (
<InputField>
<InputWrapper>
<Label>1st number</Label>
<Input name="user_number_1" type="number" maxlength="2" value=this.state.user_number_1 onChange=this.inputHandler required></Input>
<Label>2nd number</Label>
<Input name="user_number_2" type="number" maxlength="2" value=this.state.user_number_2 onChange=this.inputHandler required></Input>
<Label>3rd number</Label>
<Input name="user_number_3" type="number" maxlength="2" value=this.state.user_number_3 onChange=this.inputHandler required></Input>
<Label>4th number</Label>
<Input name="user_number_4" type="number" maxlength="2" value=this.state.user_number_4 onChange=this.inputHandler required></Input>
<Label>5th number</Label>
<Input name="user_number_5" type="number" maxlength="2" value=this.state.user_number_5 onChange=this.inputHandler required></Input>
<Label>6th number</Label>
<Input name="user_number_6" type="number" maxlength="2" value=this.state.user_number_6 onChange=this.inputHandler required></Input>
<Label>Number of draws:</Label>
<Input name="draw_number" type="number" value=this.state.draw_number onChange=this.inputHandler required></Input>
</InputWrapper>
<Button type="submit" /*onClick=this.drawsHandler*/>Let's win!</Button>
</InputField>
)
javascript reactjs
Do you want to put a new value inthis.state.results.numbersarray every time an input changes?
– Tholle
Mar 27 at 11:32
This code is wrong on so many levels. You are trying to mutate state by changing it manually. The implementation of the controlled component is broken. You are changing the event of a controlled component to change another variable.
– Dehan de Croos
Mar 27 at 11:58
add a comment |
I'm currently trying to push my state elements (user_number_X) into an array (this.state.results.numbers) but my input handler function is not working.
How can I rewrite it so it will push input values into an array?
Here is my code:
export default class InputArea extends React.Component
constructor(props)
super(props);
this.state =
draw_number: '',
user_number_1: '',
user_number_2: '',
user_number_3: '',
user_number_4: '',
user_number_5: '',
user_number_6: '',
results :
numbers: [],
draws: ''
/* handledrawsChange = event =>
this.setState(
event
)
*/
inputHandler = event =>
this.setState([event.target.name]: event.target.value);
this.state.results.numbers.push(event.target.value)
// this.state.results.numbers.push(event.target.value)//this.setState(number: this.state.results.numbers.push(parseInt(event.target.value)));
componentDidMount()
fetch("http://localhost:8080/simulate",
method: "POST",
body: this.state.results
);
render()
console.log(this.state);
return (
<InputField>
<InputWrapper>
<Label>1st number</Label>
<Input name="user_number_1" type="number" maxlength="2" value=this.state.user_number_1 onChange=this.inputHandler required></Input>
<Label>2nd number</Label>
<Input name="user_number_2" type="number" maxlength="2" value=this.state.user_number_2 onChange=this.inputHandler required></Input>
<Label>3rd number</Label>
<Input name="user_number_3" type="number" maxlength="2" value=this.state.user_number_3 onChange=this.inputHandler required></Input>
<Label>4th number</Label>
<Input name="user_number_4" type="number" maxlength="2" value=this.state.user_number_4 onChange=this.inputHandler required></Input>
<Label>5th number</Label>
<Input name="user_number_5" type="number" maxlength="2" value=this.state.user_number_5 onChange=this.inputHandler required></Input>
<Label>6th number</Label>
<Input name="user_number_6" type="number" maxlength="2" value=this.state.user_number_6 onChange=this.inputHandler required></Input>
<Label>Number of draws:</Label>
<Input name="draw_number" type="number" value=this.state.draw_number onChange=this.inputHandler required></Input>
</InputWrapper>
<Button type="submit" /*onClick=this.drawsHandler*/>Let's win!</Button>
</InputField>
)
javascript reactjs
I'm currently trying to push my state elements (user_number_X) into an array (this.state.results.numbers) but my input handler function is not working.
How can I rewrite it so it will push input values into an array?
Here is my code:
export default class InputArea extends React.Component
constructor(props)
super(props);
this.state =
draw_number: '',
user_number_1: '',
user_number_2: '',
user_number_3: '',
user_number_4: '',
user_number_5: '',
user_number_6: '',
results :
numbers: [],
draws: ''
/* handledrawsChange = event =>
this.setState(
event
)
*/
inputHandler = event =>
this.setState([event.target.name]: event.target.value);
this.state.results.numbers.push(event.target.value)
// this.state.results.numbers.push(event.target.value)//this.setState(number: this.state.results.numbers.push(parseInt(event.target.value)));
componentDidMount()
fetch("http://localhost:8080/simulate",
method: "POST",
body: this.state.results
);
render()
console.log(this.state);
return (
<InputField>
<InputWrapper>
<Label>1st number</Label>
<Input name="user_number_1" type="number" maxlength="2" value=this.state.user_number_1 onChange=this.inputHandler required></Input>
<Label>2nd number</Label>
<Input name="user_number_2" type="number" maxlength="2" value=this.state.user_number_2 onChange=this.inputHandler required></Input>
<Label>3rd number</Label>
<Input name="user_number_3" type="number" maxlength="2" value=this.state.user_number_3 onChange=this.inputHandler required></Input>
<Label>4th number</Label>
<Input name="user_number_4" type="number" maxlength="2" value=this.state.user_number_4 onChange=this.inputHandler required></Input>
<Label>5th number</Label>
<Input name="user_number_5" type="number" maxlength="2" value=this.state.user_number_5 onChange=this.inputHandler required></Input>
<Label>6th number</Label>
<Input name="user_number_6" type="number" maxlength="2" value=this.state.user_number_6 onChange=this.inputHandler required></Input>
<Label>Number of draws:</Label>
<Input name="draw_number" type="number" value=this.state.draw_number onChange=this.inputHandler required></Input>
</InputWrapper>
<Button type="submit" /*onClick=this.drawsHandler*/>Let's win!</Button>
</InputField>
)
javascript reactjs
javascript reactjs
asked Mar 27 at 11:30
LeoceteLeocete
10510 bronze badges
10510 bronze badges
Do you want to put a new value inthis.state.results.numbersarray every time an input changes?
– Tholle
Mar 27 at 11:32
This code is wrong on so many levels. You are trying to mutate state by changing it manually. The implementation of the controlled component is broken. You are changing the event of a controlled component to change another variable.
– Dehan de Croos
Mar 27 at 11:58
add a comment |
Do you want to put a new value inthis.state.results.numbersarray every time an input changes?
– Tholle
Mar 27 at 11:32
This code is wrong on so many levels. You are trying to mutate state by changing it manually. The implementation of the controlled component is broken. You are changing the event of a controlled component to change another variable.
– Dehan de Croos
Mar 27 at 11:58
Do you want to put a new value in
this.state.results.numbers array every time an input changes?– Tholle
Mar 27 at 11:32
Do you want to put a new value in
this.state.results.numbers array every time an input changes?– Tholle
Mar 27 at 11:32
This code is wrong on so many levels. You are trying to mutate state by changing it manually. The implementation of the controlled component is broken. You are changing the event of a controlled component to change another variable.
– Dehan de Croos
Mar 27 at 11:58
This code is wrong on so many levels. You are trying to mutate state by changing it manually. The implementation of the controlled component is broken. You are changing the event of a controlled component to change another variable.
– Dehan de Croos
Mar 27 at 11:58
add a comment |
1 Answer
1
active
oldest
votes
You can do that using concat() and use Object Destructuring for cleaner code
const results = this.state;
const numbers = results
this.setState(results:...results,numbers:numbers.concat([event.target.value])
should I do this in the global scope or ininputHandlermethod?
– Leocete
Mar 27 at 12:39
Obviously ininputHandlerand keep the other linethis.setState([event.target.name]: event.target.value)as it is
– Maheer Ali
Mar 27 at 12:40
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add["2", "23"]to the array
– Leocete
Mar 27 at 12:47
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Which event you are using on the the<input>UseonChangein will fix the problem.
– Maheer Ali
Mar 27 at 12:56
|
show 1 more 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%2f55376141%2fhow-i-can-push-my-state-elements-to-the-state-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do that using concat() and use Object Destructuring for cleaner code
const results = this.state;
const numbers = results
this.setState(results:...results,numbers:numbers.concat([event.target.value])
should I do this in the global scope or ininputHandlermethod?
– Leocete
Mar 27 at 12:39
Obviously ininputHandlerand keep the other linethis.setState([event.target.name]: event.target.value)as it is
– Maheer Ali
Mar 27 at 12:40
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add["2", "23"]to the array
– Leocete
Mar 27 at 12:47
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Which event you are using on the the<input>UseonChangein will fix the problem.
– Maheer Ali
Mar 27 at 12:56
|
show 1 more comment
You can do that using concat() and use Object Destructuring for cleaner code
const results = this.state;
const numbers = results
this.setState(results:...results,numbers:numbers.concat([event.target.value])
should I do this in the global scope or ininputHandlermethod?
– Leocete
Mar 27 at 12:39
Obviously ininputHandlerand keep the other linethis.setState([event.target.name]: event.target.value)as it is
– Maheer Ali
Mar 27 at 12:40
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add["2", "23"]to the array
– Leocete
Mar 27 at 12:47
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Which event you are using on the the<input>UseonChangein will fix the problem.
– Maheer Ali
Mar 27 at 12:56
|
show 1 more comment
You can do that using concat() and use Object Destructuring for cleaner code
const results = this.state;
const numbers = results
this.setState(results:...results,numbers:numbers.concat([event.target.value])
You can do that using concat() and use Object Destructuring for cleaner code
const results = this.state;
const numbers = results
this.setState(results:...results,numbers:numbers.concat([event.target.value])
edited Mar 27 at 11:39
answered Mar 27 at 11:33
Maheer AliMaheer Ali
23.5k4 gold badges21 silver badges38 bronze badges
23.5k4 gold badges21 silver badges38 bronze badges
should I do this in the global scope or ininputHandlermethod?
– Leocete
Mar 27 at 12:39
Obviously ininputHandlerand keep the other linethis.setState([event.target.name]: event.target.value)as it is
– Maheer Ali
Mar 27 at 12:40
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add["2", "23"]to the array
– Leocete
Mar 27 at 12:47
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Which event you are using on the the<input>UseonChangein will fix the problem.
– Maheer Ali
Mar 27 at 12:56
|
show 1 more comment
should I do this in the global scope or ininputHandlermethod?
– Leocete
Mar 27 at 12:39
Obviously ininputHandlerand keep the other linethis.setState([event.target.name]: event.target.value)as it is
– Maheer Ali
Mar 27 at 12:40
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add["2", "23"]to the array
– Leocete
Mar 27 at 12:47
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Which event you are using on the the<input>UseonChangein will fix the problem.
– Maheer Ali
Mar 27 at 12:56
should I do this in the global scope or in
inputHandler method?– Leocete
Mar 27 at 12:39
should I do this in the global scope or in
inputHandler method?– Leocete
Mar 27 at 12:39
Obviously in
inputHandler and keep the other line this.setState([event.target.name]: event.target.value) as it is– Maheer Ali
Mar 27 at 12:40
Obviously in
inputHandler and keep the other line this.setState([event.target.name]: event.target.value) as it is– Maheer Ali
Mar 27 at 12:40
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add
["2", "23"] to the array– Leocete
Mar 27 at 12:47
it works, but it pushes a lot of unnecessary numbers into the array, for example, if I input 23, it will add
["2", "23"] to the array– Leocete
Mar 27 at 12:47
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Can you give link to a working code?
– Maheer Ali
Mar 27 at 12:52
@Leocete Which event you are using on the the
<input> Use onChange in will fix the problem.– Maheer Ali
Mar 27 at 12:56
@Leocete Which event you are using on the the
<input> Use onChange in will fix the problem.– Maheer Ali
Mar 27 at 12:56
|
show 1 more comment
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
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%2f55376141%2fhow-i-can-push-my-state-elements-to-the-state-array%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
Do you want to put a new value in
this.state.results.numbersarray every time an input changes?– Tholle
Mar 27 at 11:32
This code is wrong on so many levels. You are trying to mutate state by changing it manually. The implementation of the controlled component is broken. You are changing the event of a controlled component to change another variable.
– Dehan de Croos
Mar 27 at 11:58