I am using the react-addons-update
npm package with my React application and specifically want to use the update()
method for inserting an new object deep within an array of objects.
I have this structure as part of my component state:
...
constructor(props) {
super(props);
this.state = {
blocks: [
{
block_title: "some title",
items: [
{
item_title: "foo"
},
{
item_title: "bar"
}
]
},
{
...
}
]
};
}
...
and I want to insert a new item into the items
array, between the first and second item.
I have tried this:
...
insertItem(){
let obj = {
item_title: "in between foo and bar"
};
this.setState({
blocks: update(this.state.blocks, {
[0]: {
items: {$splice: [[1, 0, obj]]}
}
})
});
}
...
but this does not seem to be working (no errors are being thrown either). Any ideas?
p.s. I am using React within a Meteor application - just in case there is some quirk that prevents this helper function from working .