Documentation
Feedback
Guides
Learning Center

Learning Center
Learning Center
Creating the countdown block feature
View in Portuguese
This content was migrated from the VTEX Learning Center and is no longer being actively maintained. For the most accurate and up-to-date information, please check the official documentation.

Introduction

Now that we have covered the component's basics, it's time to implement the countdown effectively. For that, we need to use a React hook called useState.

It is called within the functional component to update and consume the component state. The state represents the component's current state. The useState returns a pair: the current state value and a function to update it.

Making your countdown work!

  1. First, we need to import a few functions and types to continue. Inside the Countdown component, import the following:


    _10
    //react/Countdown.tsx
    _10
    import React, { useState } from 'react'
    _10
    import { TimeSplit } from './typings/global'
    _10
    import { tick, getTwoDaysFromNow } from './utils/time'

    The getTwoDaysFromNow function will be used to handle edge cases. This step will explain it later.

  2. Next step is to add the state update hook (useState):


    _14
    //react/Countdown.tsx
    _14
    const Countdown: StorefrontFunctionComponent<CountdownProps> = ({ targetDate }) => {
    _14
    + const [timeRemaining, setTime] = useState<TimeSplit>({
    _14
    + hours: '00',
    _14
    + minutes: '00',
    _14
    + seconds: '00'
    _14
    + })
    _14
    _14
    return (
    _14
    <div>
    _14
    { targetDate }
    _14
    </div>
    _14
    )
    _14
    }

  3. After doing that, we'll add a default constant targetDate for the edge case where the prop is not defined. We'll use as a fallback a date defined as two days from the current date. This date is calculated using a util function that was previously imported from the /utils folder.


    _10
    //react/Countdown.tsx
    _10
    const DEFAULT_TARGET_DATE = getTwoDaysFromNow()

  4. Now, we need to add the tick function and the DEFAULT_TARGET_DATE constant to make the countdown work:


    _16
    //react/Countdown.tsx
    _16
    const Countdown: StorefrontFunctionComponent<CountdownProps> = ({ targetDate = DEFAULT_TARGET_DATE }) => {
    _16
    const [timeRemaining, setTime] = useState<TimeSplit>({
    _16
    hours: '00',
    _16
    minutes: '00',
    _16
    seconds: '00'
    _16
    })
    _16
    _16
    + tick(targetDate, setTime)
    _16
    _16
    return (
    _16
    <div>
    _16
    { targetDate }
    _16
    </div>
    _16
    )
    _16
    }

  5. Last but not least, change the h1 so that it shows the countdown that we've created. For that, we need to use the timeRemaining current state:


    _17
    //react/Countdown.tsx
    _17
    const Countdown: StorefrontFunctionComponent<CountdownProps> = ({ targetDate = DEFAULT_TARGET_DATE }) => {
    _17
    const [timeRemaining, setTime] = useState<TimeSplit>({
    _17
    hours: '00',
    _17
    minutes: '00',
    _17
    seconds: '00'
    _17
    })
    _17
    _17
    tick(targetDate, setTime)
    _17
    _17
    return (
    _17
    <div>
    _17
    - <h1>{ targetDate }</h1>
    _17
    + <h1>{ `${timeRemaining.hours}:${timeRemaining.minutes}:${timeRemaining.seconds}` }</h1>
    _17
    </div>
    _17
    )
    _17
    }

The countdown string formatting is in a HH:MM:SS format, made through an hours, minutes, and seconds splitting.

Therefore, with these changes, we'll see a real-time update of the countdown! The result on the homepage is this:

{"base64":"  ","img":{"width":3810,"height":1930,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":1461385,"url":"https://user-images.githubusercontent.com/19495917/75474406-b3c06e80-5975-11ea-82ec-89ab27504873.png"}}

{"base64":"  ","img":{"width":996,"height":596,"type":"gif","mime":"image/gif","wUnits":"px","hUnits":"px","length":18702,"url":"https://user-images.githubusercontent.com/19495917/75474511-e0748600-5975-11ea-825d-7e9a20f95362.gif"}}

Answer sheet

See answer sheet for Countdown.tsx
Countdown.tsx

_44
// store-block/react/Countdown.tsx
_44
import React, { useState } from 'react'
_44
import { TimeSplit } from './typings/global'
_44
import { tick, getTwoDaysFromNow } from './utils/time'
_44
_44
interface CountdownProps {
_44
targetDate: string
_44
}
_44
_44
const DEFAULT_TARGET_DATE = getTwoDaysFromNow()
_44
_44
const Countdown: StorefrontFunctionComponent<CountdownProps> = ({
_44
targetDate = DEFAULT_TARGET_DATE,
_44
}) => {
_44
const [timeRemaining, setTime] = useState<TimeSplit>({
_44
hours: '00',
_44
minutes: '00',
_44
seconds: '00',
_44
})
_44
_44
tick(targetDate, setTime)
_44
_44
return (
_44
<div>
_44
<h1>{`${timeRemaining.hours}:${timeRemaining.minutes}:${timeRemaining.seconds}`}</h1>
_44
</div>
_44
)
_44
}
_44
_44
Countdown.schema = {
_44
title: 'editor.countdown.title',
_44
description: 'editor.countdown.description',
_44
type: 'object',
_44
properties: {
_44
targetDate: {
_44
title: 'Data final',
_44
description: 'Data final utilizada no contador',
_44
type: 'string',
_44
default: null,
_44
},
_44
},
_44
}
_44
_44
export default Countdown

On this page