Adding items to an array in javascript
Admit it. Being new to JavaScript in 2018 is difficult. Coming from languages like C#, Java and Typescript(yeah subset of js..) where type safety is key, Javascript just keep f***** me over. I struggle with something simple like updating an array..
So I have this React
component, where the state is defined like this:
class Form extends React.Component
constructor(props)
super(props);
this.state =
show: false,
shoes:
;
....
...
The shoes is an array of undefined(?)
This array is passed to a stateless component which looks like this
const Shoelist = props =>
return (
<Wrapper>
props.shoes.map((shoe, i) => (
<div key=i>
<Shoe shoe=shoe />
<Separator />
</div>
))
</Wrapper>
);
;
I in my Form
component, I have a method which is supposed to react(doh) on onClick
methods. In this method I get a parameter with a new shoe to add in this list. This is very it stops for me in javascript - something which is faaaaairly easy in all other languages that we've being using for the past years..
I've tried several ways:
1#
addShoe(shoe)
this.setState(state =>
const list = state.shoes.push(shoe);
return
list
;
);
This results in an error: Uncaught TypeError: Cannot read property 'push' of undefined
Do I need to define shoes as an Array
? I thought the was enough
2#
I googled, I do that alot. I found one blog post saying something about react-addons-update
. I installed this by running yarn add
and code looks like this:
addShoe(shoe)
this.setState(update(this.state, shoes: $push: [shoe] ));
which results in Uncaught Error: update(): expected target of $push to be an array; got undefined.
Help! How difficult can this be?
EDIT
I pass this method into another component like this:
<ShoeModal onClose=this.addShoe />
in the ShoeModal component this is bound to a onClick method:
<FinishModalButton
onClick=this.props.onClose.bind(this, this.state.shoe)>
....
</FinishModalButton>
ShoeModal.propTypes =
onClose: PropTypes.func.isRequired
;
javascript reactjs
add a comment |
Admit it. Being new to JavaScript in 2018 is difficult. Coming from languages like C#, Java and Typescript(yeah subset of js..) where type safety is key, Javascript just keep f***** me over. I struggle with something simple like updating an array..
So I have this React
component, where the state is defined like this:
class Form extends React.Component
constructor(props)
super(props);
this.state =
show: false,
shoes:
;
....
...
The shoes is an array of undefined(?)
This array is passed to a stateless component which looks like this
const Shoelist = props =>
return (
<Wrapper>
props.shoes.map((shoe, i) => (
<div key=i>
<Shoe shoe=shoe />
<Separator />
</div>
))
</Wrapper>
);
;
I in my Form
component, I have a method which is supposed to react(doh) on onClick
methods. In this method I get a parameter with a new shoe to add in this list. This is very it stops for me in javascript - something which is faaaaairly easy in all other languages that we've being using for the past years..
I've tried several ways:
1#
addShoe(shoe)
this.setState(state =>
const list = state.shoes.push(shoe);
return
list
;
);
This results in an error: Uncaught TypeError: Cannot read property 'push' of undefined
Do I need to define shoes as an Array
? I thought the was enough
2#
I googled, I do that alot. I found one blog post saying something about react-addons-update
. I installed this by running yarn add
and code looks like this:
addShoe(shoe)
this.setState(update(this.state, shoes: $push: [shoe] ));
which results in Uncaught Error: update(): expected target of $push to be an array; got undefined.
Help! How difficult can this be?
EDIT
I pass this method into another component like this:
<ShoeModal onClose=this.addShoe />
in the ShoeModal component this is bound to a onClick method:
<FinishModalButton
onClick=this.props.onClose.bind(this, this.state.shoe)>
....
</FinishModalButton>
ShoeModal.propTypes =
onClose: PropTypes.func.isRequired
;
javascript reactjs
3
This is not an answer, but an observation of something that might bite you later..push
doesn't return the array - it returns the count. So I thinkconst list = state.shoes.push(shoe);
won't give you what you expect later.
– Frank Modica
Nov 14 '18 at 13:10
Can you show howaddShoe
is called?
– Frank Modica
Nov 14 '18 at 13:18
I've updated the question. Please let me know if you need anything else
– Tobias Moe Thorstensen
Nov 14 '18 at 13:24
add a comment |
Admit it. Being new to JavaScript in 2018 is difficult. Coming from languages like C#, Java and Typescript(yeah subset of js..) where type safety is key, Javascript just keep f***** me over. I struggle with something simple like updating an array..
So I have this React
component, where the state is defined like this:
class Form extends React.Component
constructor(props)
super(props);
this.state =
show: false,
shoes:
;
....
...
The shoes is an array of undefined(?)
This array is passed to a stateless component which looks like this
const Shoelist = props =>
return (
<Wrapper>
props.shoes.map((shoe, i) => (
<div key=i>
<Shoe shoe=shoe />
<Separator />
</div>
))
</Wrapper>
);
;
I in my Form
component, I have a method which is supposed to react(doh) on onClick
methods. In this method I get a parameter with a new shoe to add in this list. This is very it stops for me in javascript - something which is faaaaairly easy in all other languages that we've being using for the past years..
I've tried several ways:
1#
addShoe(shoe)
this.setState(state =>
const list = state.shoes.push(shoe);
return
list
;
);
This results in an error: Uncaught TypeError: Cannot read property 'push' of undefined
Do I need to define shoes as an Array
? I thought the was enough
2#
I googled, I do that alot. I found one blog post saying something about react-addons-update
. I installed this by running yarn add
and code looks like this:
addShoe(shoe)
this.setState(update(this.state, shoes: $push: [shoe] ));
which results in Uncaught Error: update(): expected target of $push to be an array; got undefined.
Help! How difficult can this be?
EDIT
I pass this method into another component like this:
<ShoeModal onClose=this.addShoe />
in the ShoeModal component this is bound to a onClick method:
<FinishModalButton
onClick=this.props.onClose.bind(this, this.state.shoe)>
....
</FinishModalButton>
ShoeModal.propTypes =
onClose: PropTypes.func.isRequired
;
javascript reactjs
Admit it. Being new to JavaScript in 2018 is difficult. Coming from languages like C#, Java and Typescript(yeah subset of js..) where type safety is key, Javascript just keep f***** me over. I struggle with something simple like updating an array..
So I have this React
component, where the state is defined like this:
class Form extends React.Component
constructor(props)
super(props);
this.state =
show: false,
shoes:
;
....
...
The shoes is an array of undefined(?)
This array is passed to a stateless component which looks like this
const Shoelist = props =>
return (
<Wrapper>
props.shoes.map((shoe, i) => (
<div key=i>
<Shoe shoe=shoe />
<Separator />
</div>
))
</Wrapper>
);
;
I in my Form
component, I have a method which is supposed to react(doh) on onClick
methods. In this method I get a parameter with a new shoe to add in this list. This is very it stops for me in javascript - something which is faaaaairly easy in all other languages that we've being using for the past years..
I've tried several ways:
1#
addShoe(shoe)
this.setState(state =>
const list = state.shoes.push(shoe);
return
list
;
);
This results in an error: Uncaught TypeError: Cannot read property 'push' of undefined
Do I need to define shoes as an Array
? I thought the was enough
2#
I googled, I do that alot. I found one blog post saying something about react-addons-update
. I installed this by running yarn add
and code looks like this:
addShoe(shoe)
this.setState(update(this.state, shoes: $push: [shoe] ));
which results in Uncaught Error: update(): expected target of $push to be an array; got undefined.
Help! How difficult can this be?
EDIT
I pass this method into another component like this:
<ShoeModal onClose=this.addShoe />
in the ShoeModal component this is bound to a onClick method:
<FinishModalButton
onClick=this.props.onClose.bind(this, this.state.shoe)>
....
</FinishModalButton>
ShoeModal.propTypes =
onClose: PropTypes.func.isRequired
;
javascript reactjs
javascript reactjs
edited Nov 14 '18 at 13:24
Tobias Moe Thorstensen
asked Nov 14 '18 at 13:03
Tobias Moe ThorstensenTobias Moe Thorstensen
5,1471063114
5,1471063114
3
This is not an answer, but an observation of something that might bite you later..push
doesn't return the array - it returns the count. So I thinkconst list = state.shoes.push(shoe);
won't give you what you expect later.
– Frank Modica
Nov 14 '18 at 13:10
Can you show howaddShoe
is called?
– Frank Modica
Nov 14 '18 at 13:18
I've updated the question. Please let me know if you need anything else
– Tobias Moe Thorstensen
Nov 14 '18 at 13:24
add a comment |
3
This is not an answer, but an observation of something that might bite you later..push
doesn't return the array - it returns the count. So I thinkconst list = state.shoes.push(shoe);
won't give you what you expect later.
– Frank Modica
Nov 14 '18 at 13:10
Can you show howaddShoe
is called?
– Frank Modica
Nov 14 '18 at 13:18
I've updated the question. Please let me know if you need anything else
– Tobias Moe Thorstensen
Nov 14 '18 at 13:24
3
3
This is not an answer, but an observation of something that might bite you later.
.push
doesn't return the array - it returns the count. So I think const list = state.shoes.push(shoe);
won't give you what you expect later.– Frank Modica
Nov 14 '18 at 13:10
This is not an answer, but an observation of something that might bite you later.
.push
doesn't return the array - it returns the count. So I think const list = state.shoes.push(shoe);
won't give you what you expect later.– Frank Modica
Nov 14 '18 at 13:10
Can you show how
addShoe
is called?– Frank Modica
Nov 14 '18 at 13:18
Can you show how
addShoe
is called?– Frank Modica
Nov 14 '18 at 13:18
I've updated the question. Please let me know if you need anything else
– Tobias Moe Thorstensen
Nov 14 '18 at 13:24
I've updated the question. Please let me know if you need anything else
– Tobias Moe Thorstensen
Nov 14 '18 at 13:24
add a comment |
3 Answers
3
active
oldest
votes
With your updates we can see that the issue is the way the addShoe
callback is passed. It's being invoked as a function instead of a method of an object, so it loses context.
Change it to:
<ShoeModal onClose=this.addShoe.bind(this) />
or
<ShoeModal onClose=shoe => this.addShoe(shoe) />
In addition, .push
returns the count of the array, so the following line won't give you what you expect:
const list = state.shoes.push(shoe);
See @merko's answer for a solution.
add a comment |
You can do it this way:
this.setState(
shoes: [...this.state.shoes, newShoe]
)
...
adds all elements from this.state.shoes
1
It's called spread
– Oram
Nov 14 '18 at 13:12
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
1
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
|
show 2 more comments
Firstly, your addShoe
method is not an arrow function.
Using arrow functions because the context this
is of the component.
Second, you are returning the object list
. This sets the variable list
in state.
Also push to the new list
variable instead of mutating state.
Change your function to
addShoe = (shoe) =>
this.setState(state =>
let list = state.shoes;
list.push(shoe);
return
shoes : list
;
);
We don't know how he's callingaddShoe
yet. He could be doing it with an arrow function or.bind
. TheaddShoe
method on the class doesn't necessarily have to be an arrow function.
– Frank Modica
Nov 14 '18 at 13:20
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
1
Ok now with the update we can say that the way the method is passed is a problem.<ShoeModal onClose=this.addShoe />
should probably be<ShoeModal onClose=this.addShoe.bind(this) />
or<ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
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%2f53300909%2fadding-items-to-an-array-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
With your updates we can see that the issue is the way the addShoe
callback is passed. It's being invoked as a function instead of a method of an object, so it loses context.
Change it to:
<ShoeModal onClose=this.addShoe.bind(this) />
or
<ShoeModal onClose=shoe => this.addShoe(shoe) />
In addition, .push
returns the count of the array, so the following line won't give you what you expect:
const list = state.shoes.push(shoe);
See @merko's answer for a solution.
add a comment |
With your updates we can see that the issue is the way the addShoe
callback is passed. It's being invoked as a function instead of a method of an object, so it loses context.
Change it to:
<ShoeModal onClose=this.addShoe.bind(this) />
or
<ShoeModal onClose=shoe => this.addShoe(shoe) />
In addition, .push
returns the count of the array, so the following line won't give you what you expect:
const list = state.shoes.push(shoe);
See @merko's answer for a solution.
add a comment |
With your updates we can see that the issue is the way the addShoe
callback is passed. It's being invoked as a function instead of a method of an object, so it loses context.
Change it to:
<ShoeModal onClose=this.addShoe.bind(this) />
or
<ShoeModal onClose=shoe => this.addShoe(shoe) />
In addition, .push
returns the count of the array, so the following line won't give you what you expect:
const list = state.shoes.push(shoe);
See @merko's answer for a solution.
With your updates we can see that the issue is the way the addShoe
callback is passed. It's being invoked as a function instead of a method of an object, so it loses context.
Change it to:
<ShoeModal onClose=this.addShoe.bind(this) />
or
<ShoeModal onClose=shoe => this.addShoe(shoe) />
In addition, .push
returns the count of the array, so the following line won't give you what you expect:
const list = state.shoes.push(shoe);
See @merko's answer for a solution.
answered Nov 14 '18 at 13:29
Frank ModicaFrank Modica
6,3042727
6,3042727
add a comment |
add a comment |
You can do it this way:
this.setState(
shoes: [...this.state.shoes, newShoe]
)
...
adds all elements from this.state.shoes
1
It's called spread
– Oram
Nov 14 '18 at 13:12
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
1
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
|
show 2 more comments
You can do it this way:
this.setState(
shoes: [...this.state.shoes, newShoe]
)
...
adds all elements from this.state.shoes
1
It's called spread
– Oram
Nov 14 '18 at 13:12
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
1
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
|
show 2 more comments
You can do it this way:
this.setState(
shoes: [...this.state.shoes, newShoe]
)
...
adds all elements from this.state.shoes
You can do it this way:
this.setState(
shoes: [...this.state.shoes, newShoe]
)
...
adds all elements from this.state.shoes
answered Nov 14 '18 at 13:10
merkomerko
284412
284412
1
It's called spread
– Oram
Nov 14 '18 at 13:12
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
1
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
|
show 2 more comments
1
It's called spread
– Oram
Nov 14 '18 at 13:12
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
1
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
1
1
It's called spread
– Oram
Nov 14 '18 at 13:12
It's called spread
– Oram
Nov 14 '18 at 13:12
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Thanks for you answer, merko. But the result is: Uncaught TypeError: Invalid attempt to spread non-iterable instance. Do I need to install any packages from yarn?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:13
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Could you console.log(this.state.shoes), seems like it's not an array or an object ?
– merko
Nov 14 '18 at 13:14
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
Seems that you are correct. The output in chrome is: undefined
– Tobias Moe Thorstensen
Nov 14 '18 at 13:18
1
1
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
Here is the simple app: stackblitz.com/edit/react-2svott Hope it helps, ask anything you need
– merko
Nov 14 '18 at 13:24
|
show 2 more comments
Firstly, your addShoe
method is not an arrow function.
Using arrow functions because the context this
is of the component.
Second, you are returning the object list
. This sets the variable list
in state.
Also push to the new list
variable instead of mutating state.
Change your function to
addShoe = (shoe) =>
this.setState(state =>
let list = state.shoes;
list.push(shoe);
return
shoes : list
;
);
We don't know how he's callingaddShoe
yet. He could be doing it with an arrow function or.bind
. TheaddShoe
method on the class doesn't necessarily have to be an arrow function.
– Frank Modica
Nov 14 '18 at 13:20
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
1
Ok now with the update we can say that the way the method is passed is a problem.<ShoeModal onClose=this.addShoe />
should probably be<ShoeModal onClose=this.addShoe.bind(this) />
or<ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
add a comment |
Firstly, your addShoe
method is not an arrow function.
Using arrow functions because the context this
is of the component.
Second, you are returning the object list
. This sets the variable list
in state.
Also push to the new list
variable instead of mutating state.
Change your function to
addShoe = (shoe) =>
this.setState(state =>
let list = state.shoes;
list.push(shoe);
return
shoes : list
;
);
We don't know how he's callingaddShoe
yet. He could be doing it with an arrow function or.bind
. TheaddShoe
method on the class doesn't necessarily have to be an arrow function.
– Frank Modica
Nov 14 '18 at 13:20
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
1
Ok now with the update we can say that the way the method is passed is a problem.<ShoeModal onClose=this.addShoe />
should probably be<ShoeModal onClose=this.addShoe.bind(this) />
or<ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
add a comment |
Firstly, your addShoe
method is not an arrow function.
Using arrow functions because the context this
is of the component.
Second, you are returning the object list
. This sets the variable list
in state.
Also push to the new list
variable instead of mutating state.
Change your function to
addShoe = (shoe) =>
this.setState(state =>
let list = state.shoes;
list.push(shoe);
return
shoes : list
;
);
Firstly, your addShoe
method is not an arrow function.
Using arrow functions because the context this
is of the component.
Second, you are returning the object list
. This sets the variable list
in state.
Also push to the new list
variable instead of mutating state.
Change your function to
addShoe = (shoe) =>
this.setState(state =>
let list = state.shoes;
list.push(shoe);
return
shoes : list
;
);
edited Nov 14 '18 at 13:20
answered Nov 14 '18 at 13:18
Nishant NairNishant Nair
12
12
We don't know how he's callingaddShoe
yet. He could be doing it with an arrow function or.bind
. TheaddShoe
method on the class doesn't necessarily have to be an arrow function.
– Frank Modica
Nov 14 '18 at 13:20
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
1
Ok now with the update we can say that the way the method is passed is a problem.<ShoeModal onClose=this.addShoe />
should probably be<ShoeModal onClose=this.addShoe.bind(this) />
or<ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
add a comment |
We don't know how he's callingaddShoe
yet. He could be doing it with an arrow function or.bind
. TheaddShoe
method on the class doesn't necessarily have to be an arrow function.
– Frank Modica
Nov 14 '18 at 13:20
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
1
Ok now with the update we can say that the way the method is passed is a problem.<ShoeModal onClose=this.addShoe />
should probably be<ShoeModal onClose=this.addShoe.bind(this) />
or<ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
We don't know how he's calling
addShoe
yet. He could be doing it with an arrow function or .bind
. The addShoe
method on the class doesn't necessarily have to be an arrow function.– Frank Modica
Nov 14 '18 at 13:20
We don't know how he's calling
addShoe
yet. He could be doing it with an arrow function or .bind
. The addShoe
method on the class doesn't necessarily have to be an arrow function.– Frank Modica
Nov 14 '18 at 13:20
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
Doing it with a bind.
– Tobias Moe Thorstensen
Nov 14 '18 at 13:25
1
1
Ok now with the update we can say that the way the method is passed is a problem.
<ShoeModal onClose=this.addShoe />
should probably be <ShoeModal onClose=this.addShoe.bind(this) />
or <ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
Ok now with the update we can say that the way the method is passed is a problem.
<ShoeModal onClose=this.addShoe />
should probably be <ShoeModal onClose=this.addShoe.bind(this) />
or <ShoeModal onClose=shoe => this.addShoe(shoe) />
– Frank Modica
Nov 14 '18 at 13:26
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
@FrankModica magic! Can you post that as an answer and I'll accept it?
– Tobias Moe Thorstensen
Nov 14 '18 at 13:28
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.
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%2f53300909%2fadding-items-to-an-array-in-javascript%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
3
This is not an answer, but an observation of something that might bite you later.
.push
doesn't return the array - it returns the count. So I thinkconst list = state.shoes.push(shoe);
won't give you what you expect later.– Frank Modica
Nov 14 '18 at 13:10
Can you show how
addShoe
is called?– Frank Modica
Nov 14 '18 at 13:18
I've updated the question. Please let me know if you need anything else
– Tobias Moe Thorstensen
Nov 14 '18 at 13:24