0

I am new in reactjs. I have problem while showing the result.Here is my code:

constructor (props) {
      super(props);
       this.state = {
         selectValue: '',
         yearsValue:'',
         months:''
       }
     this.setDataGroup = this.setDataGroup.bind(this);
   }

setDataGroup(e){
    var selectedType = e.target.value;
    if(selectedType == "quertly"){
       var quertlyData = [{
        data:"Jan-March",
        value:"1"
      },
      {
        data:"Apr-Jun",
        value:"2"
      },
      {
        data:"Jul-Sept",
        value:"3"
      },
      {
        data:"Oct-Dec",
        value:"4"
      }];
       this.setState({yearsValue:quertlyData});

I am showing the data like:

 render() {
 const data = JSON.stringify(this.state.yearsValue) || []
                  <div>
    {data.map((data, index) => {data})}
  </div>
}

My json looks like:

[{"data":"Jan-March","value":"1"},{"data":"Apr-Jun","value":"2"},{"data":"Jul-Sept","value":"3"},{"data":"Oct-Dec","value":"4"}]

Getting error .map is not function

2
  • 1
    why stringify? the data is an array already and I am assuming you don't want to get it as a string. Also your render method is mute, what do you expect from the mapping function, since the arrow function will return undefined for all elements Commented Nov 13, 2017 at 12:11
  • yearsValue is a string. JSON.stringify won't turn it into an array. Commented Nov 13, 2017 at 12:12

2 Answers 2

3

yearsValue is a string and therefore using JSON.stringify won't help you.

Also, render method must return something:

constructor (props) {
  super(props);
  this.state = {
    selectValue: '',
    yearsValue: [],
    months:''
  }

  this.setDataGroup = this.setDataGroup.bind(this);
}
... 
render() {
  const { yearsValue } = this.state

  return (
    <div>
      {yearsValue.map((item, index) =>
        <div key={index}>{item.data}:{item.value}</div>
      )}
    </div>
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

yearsValue is an array, if it would be a string JSON.parse would be helpful
So it's better to user proper types than trying to parse on every render. Performance-wise this is a bad patern.
Yeah, I believe the OPs confusion is surrounding his constructor setup, and the setState method inside his setDataGroup function
Agree. That's why I suggested to initialize yearsValue as an array in the constructor.
@Karan did you change it in the constructor as well? It has to be an array, otherwise it will always break.
2

Your data is in correct format. Initialize yearsValue as array in constructor like

this.state = {
  yearsValue: []
}

And then you can use yearsValue state in render like below

render() {
  return (
     <div>
       {this.state.yearsValue.map((d, index) => {
          return (
             <div key={index}>{d.data}</div>
          )
       })}
     </div>
  )
}

5 Comments

Presumably yearsValue could be an empty string, which wouldn't have map
@Icepickle - Yes correct. I have updated the answer..Hope that might work.
Well, almost, the render method doesn't return anything yet ;)
You are close. Just missed the return from render
Thanks @mersocarlin

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.