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;








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>
)











share|improve this question
























  • 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

















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>
)











share|improve this question
























  • 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













0












0








0


1






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>
)











share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 11:30









LeoceteLeocete

10510 bronze badges




10510 bronze badges















  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















1














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])





share|improve this answer



























  • 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











  • 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> Use onChange in will fix the problem.

    – Maheer Ali
    Mar 27 at 12:56










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
);



);













draft saved

draft discarded


















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









1














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])





share|improve this answer



























  • 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











  • 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> Use onChange in will fix the problem.

    – Maheer Ali
    Mar 27 at 12:56















1














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])





share|improve this answer



























  • 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











  • 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> Use onChange in will fix the problem.

    – Maheer Ali
    Mar 27 at 12:56













1












1








1







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])





share|improve this answer















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])






share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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> Use onChange in 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











  • 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











  • @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
















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








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.



















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.




draft saved


draft discarded














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





















































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

Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴