So far I have GET_TOURNAMENTS
that sets all tournament objects in my state, and I render em on the page.
Then, I gave each tournament a button that calls showTournament()
which selects whichever tournament I click, and updates the state. When I click SHOW_TOURNAMENT
my state looks like:
tournament
tournaments: [{...}, {...}] <<~~this is from GET_TOURNAMENTS
showTournament: { <<~~begins as (showTournament: "") until showTournament() action
_id: "etc",
title: "Tournament One",
status: "Open",
participants: [ <<~~an array of User objects
0: {_id: "asdf", username: "asdf"},
1: {_id: "asdf", username: "asdf"}
]
}
Annnnnnnnd I've attempted to render all of this in my ShowTournament component by doing:
class TournamentShow extends Component {
static propTypes = {
tournament: PropTypes.object.isRequired,
auth: PropTypes.object.isRequired
};
render() {
const { _id, title, hostedBy, status, participants } = this.props.tournament.showTournament;
return (
<div>
<h1>{ title }</h1>
<p>status: { status }</p>
<p>Registered Participants:</p>
{
participants ?
participants.forEach(participant => {
return (
<ul>
<li>{participant}</li>
</ul>
)
}) :
null
}
<p>Hosted by: { hostedBy }</p>
<Link to="#">Sign Up</Link><br/>
<Link to="/">Back to Tournaments main page</Link>
</div>
)
}
};
const mapStateToProps = state => ({
tournament: state.tournament,
auth: state.auth
});
export default connect(mapStateToProps, { showTournament })(TournamentShow);
This shows nothing. Nothing is rendered even if there are Participants in the array.
I also tried simply <<~~ EDITED: I had the tags inside at first, put them outside the {}
<p>Registered Participants:</p>
<ul>
{
participants.forEach(participant => {
return (
<li>{participant}</li>
)
})
}
</ul>
to which I get the error TypeError: Cannot read property 'forEach' of undefined
All the non-array data from the showTournament
renders just fine.. I have found other Stack Overflow questions and tried a number of solutions but other questions are just different enough that I can't figure out how to get the right implementation.
Thanks all!