0

I want to have a data structure in terms of array e.g.

data[0]
  title:"ABC"
  photo: "./123.jpg"
  summ: "text text test"
data[1]
  title:"ABCD"
  photo: "./1234.jpg"
  summ: "text texwt test"

I have attempted to setState like this, but it won't work. Please guide me ;(


             this.setState({
               data:{
                title: [ ...this.state.data.title, title ],
                photo: [ ...this.state.data.photo, photo ],
                summ: [ ...this.state.data.summ, summ ]   

                    }

                  });

Uncaught (in promise) TypeError: _this.state.data.concat is not a function

enter image description here

///// Mistake on not initialize my state

// this is incorrect
this.state={
data:{
title:null,
photo:null,
summ:null,
}
//The correct version is
this.state={
data:[],

9
  • What do you mean by it won't work ? Commented Feb 18, 2020 at 12:52
  • 1
    You are somehow creating an object of arrays whereas you want and array of objects. You seem to want something more in the line of this.setState({data: [...this.state.data, {title, photo, summ}]}) Commented Feb 18, 2020 at 12:52
  • You said you want data to be an array. However, in setState, you are assigning data to an object Commented Feb 18, 2020 at 12:55
  • Don't give us an "e.g." of your data, show us a real definition in JavaScript. Commented Feb 18, 2020 at 12:55
  • Uncaught (in promise) TypeError: _this.state.data.concat is not a function @FelixKling Commented Feb 18, 2020 at 12:56

2 Answers 2

1

As Felix Kling said in commentary, you want an Array of Object so you need to have a structure like that :

this.setState({
  data: [
    ...this.state.data,
    { title, photo, summ }
  ];
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply but Uncaught (in promise) TypeError: _this.state.data.concat is not a function
How did you initialize your state ? Try to put this in the constructor : this.state = { data: [] }
1

The reason your solution doesn't work is that you are trying to assign an object to an array.

I am not sure if you are trying to add an item to array or change single item from an array. If you are trying to add, you can do it like this:

this.setState({
...this.state.data, {title, photo, summ}
})

However if you are trying to change an item from the array. You should store in a temporary variable and then assign it like this.

const tempArray = this.state.data;
this.setState({
    data: tempArray.filter(//some change function)
})

If you are trying to initialize your data into state, you can do it like this:

import JsonData from "./myJsonData"
constructor(props) {
super(props);
this.searchBarRef = React.createRef();
this.state = {
  data: JsonData
};

}

Hope this solves your issue. If I couldn't explain clearly please say, so I can try to give a better answer to your problem.

2 Comments

Thanks for your reply. I have a Json data, I want to use store it into a state variable and display it later provided I have two buttons A and B. A will show list A // $filter column eq 1 B will show list B // $first column eq 2 I am trying to save the data into state so when I click the button, the query is chanaged and data will be changed too
I update my answer and added how to initialize state with jsonData. @RandomI.T

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.