Yew: How to fetch something on pageload?

Created on 13 Apr 2018  路  4Comments  路  Source: yewstack/yew

Well, looking at the examples, it's easy to bind some functionality to a button. But how does one e.g. fetch data on page load?

question

Most helpful comment

Take the gravatar example:

In the gravatar message handling code, they create a callback which will be send back to the event look by the Env<Context>. This is given back to the context for fetching the gravatar data.

You also have a Env<Context> as well in the create function. Doing the equivalent will work, because this function will be called when your Component will be placed in the DOM. The rest of the Message handling can be adopted.

All 4 comments

Take the gravatar example:

In the gravatar message handling code, they create a callback which will be send back to the event look by the Env<Context>. This is given back to the context for fetching the gravatar data.

You also have a Env<Context> as well in the create function. Doing the equivalent will work, because this function will be called when your Component will be placed in the DOM. The rest of the Message handling can be adopted.

In the gravatar message handling code, they create a callback which will be send back

the link is missing, and another problem is that in the newest yew, we don't have Env<Context> anymore in the create function.

After I checked the code from gravatar handling, in fact the code fetch data after the component is created, but what we want is to load data from file when creating a component and make the data remain unchanged untill we reflesh the whole page. This use case is suitable for most website.

The dashboard example shows how to make fetch requests, although they are kicked off by a user action. In order to make it run as soon as the component is instantiated, move the relevant code inside update's FetchData variant to create or mounted to make it run immediately.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sackery picture sackery  路  3Comments

sanpii picture sanpii  路  3Comments

kellytk picture kellytk  路  3Comments

DenisKolodin picture DenisKolodin  路  5Comments

jstarry picture jstarry  路  4Comments