0

I am trying to render a simple profile from my redux store. Actually, the data is successfully provided and mapped to my props. I can log the profile information in the console. But when I render the profile data, it tells me that the data is not defined.

When I console log the profile , it says undefined first and then displays the data (see down). I wonder if react tries to render in the moment the data is undefined.

Console Log

undefined                        profile.js:28 

Object                           profile.js:28 
bio: "Test"
id: 2
image: "http://localhost:8000/media/default.jpg"

Component

export class ProfileDetail extends Component {

  constructor(props) {
    super(props);  
}

 componentDidMount() {                                                         
    this.props.getProfile(this.props.match.params.id);
  }


    static propTypes = {
      profile: PropTypes.array.isRequired,
      getProfile: PropTypes.func.isRequired,
   };

render() {  
    const profile = this.props.SingleProfile;
    console.log (profile)

    return (

  <Fragment>
        
      <div className="card card-body mt-4 mb-4">
      <h2>{profile.bio}</h2> // if i use here just a string its successfully rendering
      </div>
      </Fragment>
   
    );
  }
}

function mapStateToProps(state, ownProps) {
  const { profile} = state.profile
  const id = parseInt(ownProps.match.params.id, 10)      
  const SingleProfile = profile.find(function(e) {
    return e.id == id;
  });

  return { profile, SingleProfile}
}


export default connect(
  mapStateToProps,
  { getProfile}
)(ProfileDetail);


Im a little bit lost and happy for any clarification.

1
  • A good way to start is to check redux dev tools, see what actions are dispatched and what changes they cause on the state, then identify a problem and if you can't solve it post relevant code like dispatched this, reduced like so, connected like this but getting that. Commented Jul 6, 2020 at 18:34

1 Answer 1

1

You need to check if profile is loaded. It is normal that request takes some time - it is not instant - to load any data. Check that profile exists and display its property

<h2>{profile && profile.bio}</h2>

As an alternative, you could display a message:

<h2>{profile ? profile.bio : "Profile not loaded yet"}</h2>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer! It works. I guess the answer you provided is a quick fix and there should be more sophisticated solutions to check if the data ist actually loaded?
@dan_boy the more sophisticated way is to handle http requests at your middleware and then act accordingly. but even with this, this code snippet is "mandatory" let's say. You always need to check somehow that a variable is loaded when dealing with http requests, before displaying a property of it.
for example you could have a "messages" panel and load all http error messages there. then user would be presented with these errors in that panel. every time an http error occurs, you dispatch an equivalent action e.g. HTTP_FAILURE and then you handle it accordingly at your reducer(s).

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.