I have been working on an event signup page for a school club I am in, and I cannot seem to have my program read the contents of my array, and put each one in a ListItem, which goes in a List for the life of me.
When I call this function, the boolean expression this.state.events.length !== 0
evaluates to False, however console.log(this.state.events)
shows the array is not empty.
What might be causing this and how do you propose I go about fixing this.
Side note: I would love some criticism of my code. I just started with JS, React, and MaterialUI and would like to improve.
class EventSelector extends Component {
constructor(props){
super(props);
this.state = {
events: []
}
}
componentDidMount = () => {
var lst = [] // this is a list of events s.t. {key: "event_name"}
const eventsRef = db
.collection('events'); // this is a reference to the events collection
const offeredRef = eventsRef
.where('allowRegistration', '==', true)
.get() // this contains all docs which allow registration.
.then((querySnapshot) => {
querySnapshot.forEach((doc) => { // for each document, create key value pairs
lst.push({key: doc.data().name, id: doc.id})
})
})
// console.log(lst)
this.setState({ events: lst })
return true;
}
EventList = (events) => {
const { classes } = this.props;
return(
<div>
<Grid container item
direction='column'
alignItems='center'
justify='center'
style={{ maxHeight: '70vh', maxWidth: '50vw' }}
spacing={5}>
<Grid item>
<h1 className={classes.h1}>Upcoming Events</h1>
<h2 className={classes.h2}>Please select an event to sign up</h2>
</Grid>
<Grid container item direction='row' justify='center' spacing={5}>
<List component='b' subheader={<ListSubheader componenet='b'>Upcomming Events</ListSubheader>}>
{events.map(( {key , id} ) => {
// console.log(key)
return (
<div key={id}>
<ListItem button>
<ListItemText inset primary={key}/>
</ListItem>
</div>
);
}) }
</List>
</Grid>
</Grid>
</div>
);
}
// }
render = () => {
// const { classes, lists } = this.props;
const { classes } = this.props;
console.log(this.state.events)
var obj = Object.assign({}, this.state.events)
console.log(JSON.stringify(obj))
return (this.state.events.length !== 0 ? <h1>{JSON.stringify(this.state.events)}</h1> : <h2>Loading</h2>)
}
}
export default withStyles(styles)(EventSelector);