0

My react-redux app is getting a single record in JSON but the record is an array and therefore it looks like this (notice [ ] brackets):

{"person":[{"PersonID":1,"Name":"John Smith","Gender":0}]}

So, the redux store shows it as person->0->{"PersonID":1,"Name":"John Smith","Gender":0}. As such, the state shows that the person object is empty:

Name: this.props.person?this.props.person.Name:'object is empty',

My PersonPage.js includes the details page like this:

<PersonDetail person={this.props.person} />

The details page has this:

import React from 'react';
import classnames from 'classnames';

class PersonDetail extends React.Component {
   state = {
        Name: this.props.person?this.props.person.Name:'',
        PersonID: this.props.person?this.props.person.PersonID:null,
        loading: false,
        done: false
    }


 componentWillReceiveProps = (nextProps) => {
      this.setState({
        PersonID: nextProps.person.PersonID,
        Name: nextProps.person.Name

      });
  }

This is my raw Redux state:

people: [
    [
      {
        PersonID: 51,
        Name: 'John Smith',
        Gender: 0      
      }
    ]
  ]
3
  • you want something like people: [ {}, {}, {} ]? or just get the object from there? Commented Apr 5, 2017 at 9:46
  • I already have "people" (and that works on a list page) but for the single record, I just want "person". But I can get it to work by taking the 0 index of people, I'm fine with that too. It just doesn't seem as clean or logical. Commented Apr 5, 2017 at 9:48
  • check my answer, I think make sense to write an util function to parse the "people" Commented Apr 5, 2017 at 9:51

5 Answers 5

2

Person is an array, that contains the object in which Name key is present, so you need to use index also, write it like this:

this.props.person && this.props.person.length ? this.props.person[0].Name : '';

Check this example:

var data = {
      "person":[
                 {
                    "PersonID":1,
                    "Name":"John Smith",
                    "Gender":0
                  }
              ]
};

console.log('Name: ', data.person[0].Name);

Sign up to request clarification or add additional context in comments.

1 Comment

yes, still have the issue. I'm afraid neither answer is helping. This is my raw redux state:
0

I think that you are supposed to map the person detail foreach person's data.

on the PersonPage.js ,

map it as follows:

{
    this.props.person.map((p)=>{
    return (<PersonDetail person={p} />)
    })
}

1 Comment

looks logical, but gives me a blank page.
0

If I was you I would make an util function like this:

const parsePeople = people => {

  if (people instanceof Array) return parsePeople(people.pop())

  return people

}


const people = [
  [{
    PersonID: 51,
    Name: 'John Smith',
    Gender: 0      
  }]
]


const person = parsePeople(people)

console.log(person) -> Object

Using recursion we check if people is an instance of Array, we call the function again using people.pop() which return the last element of the array.

Comments

0

you have an array on your person data... you can only access that without the 0 using map...

example:

componentWillReceiveProps = (nextProps) => {
  var PersonID = nextProps.person ? nextProps.person.map(item => { return item.PersonID}) : ''; 
  var Name = nextProps.person ? nextProps.person.map(item => { return item.Name}) : ''; 

this.setState({
    PersonID,
    Name
  });

}

this is considering you only have 1 array on person.

2 Comments

sorry, I get a blank page with this error in the console:Uncaught TypeError: Cannot read property 'Name' of null
in that case you need to add validation on the return, since if that is the error.. your 1st render is empty yet... to correct that you need to include return item.Name?item.Name:''; instead of just return item.Name which should also be applied to PersonID..
0

I fixed it! It was a combination of two of the answers given: In the PersonPage.js, I had to call the PersonDetails object like this:

<PersonDetail
            person={this.props.person[0]}           
          />

And this is the new MapStatetoProps:

function mapStateToProps(state, props) {
  const { match } = props;
   if (match.params.PersonID) {    
         return {   
     person: state.people
    }    
 } 

Thanks to those who answered. This drove me nuts.

Comments

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.