Pull GraphQL data into gatsby-browser.js (or better solution, please)

你离开我真会死。 提交于 2019-12-06 00:06:50

Well, If your problem is the updating the component maybe the problem is in the way you are dealing with side effects (the componentDidMount fetch in TripAdvisor).

For what I can see, the data that you receive in props is all good, and it should fetch, but there is an error. componentDidMount will ONLY BE CALLED ONCE, and this time is the first time the component has been loaded in the page. So it doesnt matter that your props change and that you render again, the code in componentDidMount won't be called again.

You could do the fix in 2 ways. Rather you add key in

 <TripAdvisor key={tripAdvisorId} name={name} starRating={starRating} location={location} tripAdvisorId={tripAdvisorId} * />

This way react will unmount the previous TripAdvisor and mount a new one thereby calling the componentDidMount method again... What could be a bit of a problem if you have to be remounting everything just for a different data from the props....

I could recommend you a second way, that is also the way they recommend in the React Documentation. Instead of realizing your changes in componentDidMount, do them in componentDidUpdate(prevProps, prevState) (NOTE: You will need to safe your fetch by this.props.tripAdvisorId !== prevProps.tripAdvisorId or you will enter a infinite loop).

I think that this is the problem for you to update the data, nothing gatsby related... But comment me if the problem persist and I could check further into the GraphQL problems that you are running into.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!