Refresh quote
Our application works fine, but we force the user to refresh the page to see a new quote. Let's add a button to refresh the quote.
Insert a button
Our goal now is to add a button that, when clicked, will refresh the quote. We will use the component.replaceChild
method that allows us to replace only a portion of the component.
Let's start by replacing the src/Main.tsx
file content with the following:
In the above code, we added a button. We use it to wait for a click event and refresh the quote. The Main
component renders a button and the Quote component tags them with key
attribute: SeqFlow tracks it internally, so it knows:
- which component to replace when the quote is fetched;
- the button to wait for the click event;
After, the Main
component waits for the button click event and replaces the Quote component with the new quote.
Anyway, we can improve the above code by avoiding duplicated code. Let's see how.
Create a Spot component
The code duplication is not a good practice. We invoke getRandomQuote
twice: once when the page is loaded and once when the button is clicked.
What we want is to create a function that can be invoked when the component is mounted the first time and when the button is clicked. This function should shows the loading message, fetches the quote, and renders it.
Let's start by replacing the src/Main.tsx
file content with the following:
In the above code, we created a new component called Spot
. It is empty and tags the place where the Quote
component will be rendered. We use the key
attribute to track it.
Avoid double fetch
The above code has a subtle bug: if the user clicks the button twice before the first fetch is completed, the application will fetch the quote twice.
Using the component library provided by SeqFlow team, we can avoid this issue. We can disable the button while the quote is being fetched.
Let's fix it using the below code:
ButtonComponent
is a special component that allows you to change the button properties. It exposts transition
method that allows you to change the button properties easily. In the above code, we use it to disable the button while the quote is being fetched and show a loading spinner.
When the quote is fetched, we enable the button again.
NB: Typescript doesn't understand what the real element is. This is why we have to cast the button to ButtonComponent
(which extends HTMLButtonElement
).
Conclusion
In this tutorial, we have learned how to handle click events and avoid double fetches. We have also learned how to use the key
attribute to track the components.
Now, we have a fully functional application that shows a random quote and allows the user to refresh it by clicking a button. Well done!