0

I am very new to React-redux applications development so I am trying to code simple worldcup app. Simply my code are;

class Teams extends Component {
  componentDidMount() {
    this.props.onRequestData();
  }

  renderTeams() {
    const { team } = this.props;
    console.log(team);
            _.map(team, teamList =>
              <Grid.Column
                mobile={4}
                tablet={8}
                computer={8}
                key={teamList.id} >
                <Card >
                  <Image src={teamList.flag} />
                  <Card.Content>
                    <Card.Header>Nationality</Card.Header>
                    <Card.Description key={teamList.id}> {teamList.name} </Card.Description>
                  </Card.Content>
                </Card>
              </Grid.Column>
          );

  }
  render() {
    return (
      <Container>
        <Grid>
          {this.renderTeams()}
        </Grid>
      </Container>);
  }
}

const mapStateToProps = (state) => {
console.log("mapPropState", state);
  return {
    team: state.fetchData.teams

  };
};
const mapDispatchToProps = dispatch => ({
  onRequestData: () => dispatch(fetchResults())
});

I can see I fetch the data(with console.log("mapPropState", state) but unfortunately I guess my render method undefined. So nothings work on page and no console error. So where did i do wrong? Thank You.

3
  • What is the exact error message? And which line is it on? Commented Oct 6, 2018 at 21:32
  • One of the biggest problem is, there is no console error... I just see white page. Commented Oct 6, 2018 at 21:36
  • imgur.com/a/CaT375b you can see there what is happening. Commented Oct 6, 2018 at 21:40

2 Answers 2

2

Your renderTeams function is not returning anything. From the looks of your code, I assume it should be returning _.map(...), correct?

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

Comments

1

team is undefined when your component is first rendered. I assume that fetchResults() calls fetch() to make an HTTP request. This will eventually return the data and later you see that team has a value. You need to write your component in such a way that it checks if team is defined:

render() {
return (
  <Container>
    <Grid>
      {this.props.team && this.renderTeams()}
    </Grid>
  </Container>);

If you want to get fancy, you can render a spinning circle to indicate that data is loading until you have the data available.

Note:

The names team and teamList seem to be reversed since teamList seems to refer to a single team object in the array stored in team. I suggest you use teamList to refer to the entire array and team to refer to individual objects in the array when you map over it.

There is no need to do use _.map() because you can just do teamList.map(team => {/* ... */});. Also, you need to return the result of mapping over the list:

return teamList.map(team => {
    // ...
});

2 Comments

Simply "team &&" control works and i'll keep that in my mind what you said. Thank you so much.
@Ryoush This is a common technique called "conditional rendering". It checks if the first value is "truthy" (often meaning that it is defined) before attempting to render.

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.