Hey, I just started using React and Redux in Meteor 1.2. I’m using Redux for state management and one of its function is setting the screen name for each screen. The rootApp component is responsible for setting the screen name from mapping state to props via connect().
So I set it up that in each screen container component, the dispatch function will pass an action to ask the rootApp to set the screen name. So here’s my issue… For one of the screens, I want to get the name of the screen from a collection. I got everything working except for setting the screen name. I set the dispatch function in the componentDidMount(), but the screen name came up undefined.
I checked the other React Component APIs , such as componentWillUpdate(), componentDidUpdate(), and componentWillMount(). None of them will work. I even tried in the render(), but it threw an error at me.
I think the issue is that the dispatch function is firing before the collection is ready. What’s the best practice for getting data from a collection first and dispatching it on screen load?
Don’t be sorry! We all were new to this at some point . Asking questions is good.
Yeah I thought about putting it in the componentWillUpdate, but the problem is it will re-fetch the screen name everytime the page re-render on each update. Also there will be a discernible delay before the screen name switch from blank to the page name between initial page render and the first update.
As for your last question, doing it this way is a quite common pattern in Meteor and React architecture:
In a sense, you are right that I am ‘fetching’ before it’s ready; I created an handle called eventReady that gets a return boolean value from Meteor.subscribe that is true when it’s finished subscribing to the subs and event is a placeholder for the subs once they is ready. However if I try to access this.data.event in the render() section before eventReady is true, it will throw an error and say that ‘event’ is undefined. That’s why I added an if-statement to check if eventReady is true before I try to access ‘event’, like this:
according to the old doc, this.data is not the same as this.state when you try to manage the component state. In your case, you might want to pass this.data down to a child component, and let the child component utilize the this.props in both componentDidMount and componentDidUpdate those two areas
Yup, that’s the approach I’m thinking of taking when I upgrade to 1.3 and rewriting the app. I’ll be following the example from here: ffx-meteor-react-boilerplate