Reactjs push new element to nested state - javascript

I'm new to javascript and react, I try to push a new element to an array inside the state but there's no success.
state = {
columns: [
{
id: 122,
items: [{text:'abc'},{text:'cde'}]
},
{
id: 143,
items: []
}
]
}
addItem(columnId,text) {
const newItem = {text: text}
//this.setState(...)
}
Basically, I have an addItem function with given columnId and some text content, I want to push a new item to the items array inside the column with given columnId.
I heard that it'd be much easier with the help of immutability-helper, is that right?

You don't need any immutability helper if you learn methods like map, filter and spread syntax or Object.assign. Using some of them (the suitable ones) you can do whatever you want without mutating your state.
const addItem = (columnId, text) => {
// We are mapping the columns from the state.
const newColumns = this.state.columns.map(column => {
// If id does not match just return the column.
if (column.id !== columnId) return column;
// Else, return a new column object by using spread syntax.
// We spread the column (preserve other properties, and create items again
// using spread syntax. Spread the items, add the new text object.
return { ...column, items: [...column.items, { text }] };
});
// Lastly, set the state with newColumns.
this.setState({ columns: newColumns });
};
Without comments:
const addItem = (columnId, text) => {
const newColumns = this.state.columns.map(column => {
if (column.id !== columnId) return column;
return { ...column, items: [...column.items, { text }] };
});
this.setState({ columns: newColumns });
};

You can get value from state and push to that.
And this.setState makes re-rendering.
addItem(columnId, text) {
const newItem = {text};
let columns = this.state.columns;
let findColumn = columns.find(({id})=>id === columnId);
if( findColumn ) {
findColumn.items.push( newItem );
}
else {
columns.push({id:columnId, items:[newItem]});
}
this.setState({columns});
}
If you want tight. We can use destructuring.
addItem(columnId, text) {
let {columns} = this.state;
let findColumn = columns.find(({id})=>id === columnId);
if( findColumn ) {
findColumn.items.push( {text} );
}
else {
columns.push({id:columnId, items:[{text}]});
}
this.setState({columns});
}

You can create a copy of the state and modify it:
addItem(columnId,text) {
let newColums = [...this.state.columns]; // Create a copy of the state using spread operator
newColumns[columnId].items.push({text: text}) // Add the new item
this.setState({columns:newColumns}) // Set the state
}

addItem(columnId,text) {
const { columns } = this.state;
let newItem = columns.find( column => column.columnId === columnId);
if(newItem) {
newItem = {
...newItem,
text: text
}
} else {
newItem = {
columnId: columnId,
text: text
}
}
const newColumns = [ ...columns, newItem]
this.setState({ columns: newColumns })
}

Related

After adding in Array element change oher element but not adding to array

i've got an array:
dataSet: [
{ name: "Имя1", image: "img.jpeg", author: "Александр Полтавченко", date: "21.02.2020", id: 1 },
{ name: "Имя2", image: "img.png", author: "Александр Полтавченко", date: "21.02.2020", id: 2 },
],
addedToCart: []
and here is the function which put value from dataSet to addedToCart according ID from props:
added = (id) => {
this.setState (( { addedToCart, dataList } )=>{
const newItem = dataList.filter(el=>el.id===id);
const testArr = [...addedToCart ];
const filteredATC = testArr.filter((item, el)=>{
if(addedToCart.indexOf(item)===el){
item.count++
return item, el
}
else {
return item
}
it is works well (only one element with count ++) but if click add to another element it is just change element in array (with correct count surprisingly).
How to put another element into addedToCart, just like
[
{el1},
{el2}
]
filter returns an array instead of the desired element, you should use find instead.
I believe you would desire an approach like this:
added = (id) => {
this.setState (( { addedToCart, dataList } ) => {
const newItem = dataList.find(el=> el.id === id);
const testArr = [...addedToCart ];
const filteredATCIndex = testArr.findIndex((_item, id) => newItem.id === id)
// if there is an added item
if (filteredATCIndex !== -1) {
const count = testArr[filteredATCIndex].count + 1
testArr[filteredATCIndex] = { ...testArr[filteredATCIndex], count }
return { addedToCart: testArr }
}
// for new item
const newItemAdded = { ...newItem, count: 1 }
testArr.push(newItemAdded)
return { addedToCart: testArr }
})
}
though this approach duplicates data, which is not desirable. I suggest you consider to change addedToCart to an object where key value pairs are the id and count respectively from added items. This way you would avoid duplicating data.
then your update state would look like:
added = (id) => {
this.setState (( { addedToCart } ) => {
const count = typeof addedToCart[id] === 'undefined' ? 1 : ++addedToCart[id]
return { addedToCart: { ...addedToCart, [id]: count } }
})
}

Edit function not saving changes to state data in React

I am trying to provide functionality in my webpage for editing state data.
Here is the state structure
state = {
eventList:[
{
name: "Coachella"
list: [
{
id: 1,
name: "Eminem"
type: "rap"
}
{
id: 2,
name: "Kendrick Lamar"
type: "rap"
}
]
}
]
}
I want to be able to edit the list arrays specifically the id, name, and type properties but my function doesn't seem to edit them? I currently pass data I want to override id name and type with in variable eventData and an id value specifying which row is selected in the table which outputs the state data.
Here is the function code:
editPickEvent = (eventData, id) => {
const eventListNew = this.state.eventList;
eventListNew.map((event) => {
event.list.map((single) => {
if (single.id == id) {
single = eventData;
}
});
});
this.setState({
eventList: eventListNew,
});
};
When I run the code the function doesn't alter the single map variable and I can't seem to pinpoint the reason why. Any help would be great
edit:
Implementing Captain Mhmdrz_A's solution
editPickEvent = (eventData, id) => {
const eventListNew = this.state.eventList.map((event) => {
event.list.map((single) => {
if (single.id == id) {
single = eventData;
}
});
});
this.setState({
eventList: eventListNew,
});
};
I get a new error saying Cannot read property list of undefined in another file that uses the map function to render the state data to the table?
This is the part of the other file causing the error:
render() {
const EventsList = this.props.eventList.map((event) => {
return event.list.map((single) => {
return (
map() return a new array every time, but you are not assigning it to anything;
editPickEvent = (eventData, id) => {
const eventListNew = this.state.eventList.map((event) => {
event.list.forEach((single) => {
if (single.id == id) {
single = eventData;
}
});
return event
});
this.setState({
eventList: eventListNew,
});
};
const editPickEvent = (eventData, id) => {
const updatedEventList = this.state.eventList.map(event => {
const updatedList = event.list.map(single => {
if (single.id === id) {
return eventData;
}
return single;
});
return {...event, list: updatedList};
});
this.setState({
eventList: updatedEventList,
});
}
Example Link: https://codesandbox.io/s/crazy-lake-2q6ez
Note: You may need to add more checks in between for handling cases when values could be null or undefined.
Also, it would be good if you can add something similar to the original data source or an example link.
Turns out primitive values are pass by value in javascript, which I didn't know and why the assignment wasn't working in some of the previous suggested answers. Here is the code that got it working for me:
editEvent = (EventData, id) => {
const eventListNew = this.state.eventList.map((event) => {
const newList = event.list.map((single) => {
return single.id == id ? EventData : single;
});
return { ...event, list: newList };
});
this.setState({
eventList: eventListNew,
});
};

Insert an item in a list inside another in react state

I have this variable on my state:
this.state = {
itemList: {
item: [
{
itemAlias: [
{
name: null
}
],
idItem: null,
itemName: null,
}
]
}
}
what I want to do is to insert a new item alias on a cetain item.I did this and its inserting a new itemAlias to my item but is also creates a new Item, and I dont want that, I just want to update my item with a new Item alias:
insertAliasToList = (itm) => {
let insertedAlias = {
name: 'test'
}
itm.itemAlias.push(insertedAlias)
this.setState(prevState => ({
...prevState,
itemList: {
...prevState.itemList,
item: [...prevState.itemList.item, p]
}
}))
}
I have also tried this but the error Uncaught TypeError: Invalid attempt to spread non-iterable instance appears.
insertAliasToList = (itm) => {
let insertedAlias = {
name: 'test'
}
// itm.itemAlias.push(insertedAlias)
this.setState(prevState => ({
...prevState,
itemList: {
...prevState.itemList,
item: {
...prevState.itemLIst.item,
itemAlias:[...prevState.itemList.item.itemAlias,insertedAlias]
}
}
}))
}
Thanks in advance!
Since you are trying to replace the contents of an item within an array, you will need to first duplicate the array. Then replace what you need within that item using the index and set the state again with the new array.
let insertedAlias = {
name: 'test'
}
// Duplicate 'item' array
const item = [...this.state.itemList.item]
// Change item using index '0' on the duplicated array
item[0] = {
...item[0],
itemAlias: [
...item[0].itemAlias,
insertedAlias,
]
}
// Set state with new item array that contains changes you made
this.setState(prevState => ({
...prevState,
itemList: {
...prevState.itemList,
item: [
...item,
]
}
}))
You can do something like this:
insertAliasToList = (itm) => {
let insertedAlias = {
name: 'test'
}
this.setState(prevState => ({
...prevState,
itemList: {
...prevState.itemList,
item: prevState.itemList.item.map(i => {
if (i.idItem === itm.idItem) {
return {
...i,
itemAlias: [...i.itemAlias, insertedAlias]
}
}
return i;
})
}
}))
}
It will only work if idItem is unique.

Which approach in React is better?

Below both code does exactly same but in different way. There is an onChange event listener on an input component. In first approach I am shallow cloning the items from state then doing changes over it and once changes are done I am updating the items with clonedItems with changed property.
In second approach I didn't cloned and simply did changes on state items and then updated the state accordingly. Since directly (without setState) changing property of state doesn't call updating lifecycles in react, I feel second way is better as I am saving some overhead on cloning.
handleRateChange = (evnt: React.ChangeEvent<HTMLInputElement>) => {
const {
dataset: { type },
value,
} = evnt.target;
const { items } = this.state;
const clonedItems = Array.from(items);
clonedItems.map((ele: NetworkItem) => {
if (ele.nicType === type) {
ele.rate = Number(value);
}
});
this.setState({ items: clonedItems });
};
OR
handleRateChange = (evnt: React.ChangeEvent<HTMLInputElement>) => {
const {
dataset: { type },
value,
} = evnt.target;
const { items } = this.state;
items.map((ele: NetworkItem) => {
if (ele.nicType === type) {
ele.rate = Number(value);
}
});
this.setState({ items });
};
You can use this
this.setState(state => {
const list = state.list.map(item => item + 1);
return {
list,
};
});
if you need more info about using arrays on states, please read this: How to manage React State with Arrays
Modifying the input is generally a bad practice, however cloning in the first example is a bit of an overkill. You don't really need to clone the array to achieve immutability, how about something like that:
handleRateChange = (evnt: React.ChangeEvent<HTMLInputElement>) => {
const {
dataset: { type },
value,
} = evnt.target;
const { items } = this.state;
const processedItems = items.map((ele: NetworkItem) => {
if (ele.nicType === type) {
return {
...ele,
rate: Number(value)
};
} else {
return ele;
}
});
this.setState({ items: processedItems });
};
It can be refactored of course, I left it like this to better illustrate the idea. Which is, instead of cloning the items before mapping, or modifying its content, you can return a new object from the map's callback and assign the result to a new variable.

How do I update an array of objects in component state?

I am trying to update the property of an object which is stored in an array.
my state looks something like this:
state = {
todos: [
{
id: '1',
title: 'first item,
completed: false
},
{
id: '2',
title: 'second item,
completed: false
}
],
}
What I am trying to do is access the second element in the 'todos' array and update the completed property to either false -> true or true -> false.
I have a button with the handler for update, and my class method for the update looks like this:
onUpdate = (id) => {
const { todos } = this.state;
let i = todos.findIndex(todo => todo.id === id);
let status = todos[i].completed
let updatedTodo = {
...todos[i],
completed: !status
}
this.setState({
todos: [
...todos.slice(0, i),
updatedTodo,
...todos.slice(i + 1)
]
});
}
While this does work, I want to find out if there is a more concise way of achieving the same result; I tried to use Object.assign(), but that didn't work out because my 'todos' is an array, not an object. Please enlighten me with better code!
It would be best to use update function to make sure you don't work on outdated data:
onUpdate = (id) => {
this.setState(prevState => {
const copy = [...prevState.todos];
const index = copy.findIndex(t => t.id === id);
copy[index].completed = !copy[index].completed;
return { todos: copy }
})
}
You can simply copy your todos from state, then make edits, and after that put it back to the state
onUpdate = (id) => {
var todos = [...this.state.todos]
var target = todos.find(todo => todo.id == id)
if (target) {
target.completed = !target.completed
this.setState({ todos })
}
}

Categories

Resources