# Adding timeframe selector in CustomApp

**URL:** <https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814>\
**Category:** Developer Q&A Forum\
**Tags:** custom-app\
**Created:** [January 19, 2025, 4:33pm UTC](https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814 "2025-01-19T16:33:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanveermd](https://avatars.discourse-cdn.com/v4/letter/t/90db22/32.png) [@Tanveermd](https://community.dynatrace.com/u/Tanveermd)\
**Post date:** [January 19, 2025, 4:33pm UTC](https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814/1 "2025-01-19T16:33:55Z")

</div>

Hello there, I have created a custom app to display the trend of problems in a bar chart and how are they trending over a period of time side by side, it all looks well when built, but I think I am only able to see last 2 hours of data by default and I wanted to add timeframe selector to control the data display…I have referred&nbsp;&nbsp;[https://developer.dynatrace.com/design/components-preview/forms/TimeframeSelector/](https://developer.dynatrace.com/design/components-preview/forms/TimeframeSelector/)&nbsp;but when adding this to the code, it doesn’t appear to apply…I am fairly new to ReactJS and need help here pl.

Highlighted in bold is the code I have added referring to the Dynatrace Developer documentation but I no luck so far

import React, **{ useState }** from ‘react’;  
import { Flex } from ‘@dynatrace/strato-components/layouts’;  
import { TitleBar } from ‘@dynatrace/strato-components-preview/layouts’;  
import { ProgressCircle } from ‘@dynatrace/strato-components/content’;  
import { convertToTimeseries&nbsp;} from ‘@dynatrace/strato-components-preview/conversion-utilities’;  
import {&nbsp;DataTable,TableColumn } from ‘@dynatrace/strato-components-preview/tables’;  
import { useDqlQuery } from ‘@dynatrace-sdk/react-hooks’;  
import { PROBLEMS\_QUERY } from ‘../queries’;  
import { Colors } from ‘@dynatrace/strato-design-tokens’;  
**import type { TimeframeV2 } from ‘@dynatrace/strato-components-preview/core’;**  
**import { TimeframeSelector } from ‘@dynatrace/strato-components-preview/forms’;**

export const ProblemsList = () =\> {  
const result = useDqlQuery({  
body: {  
query: PROBLEMS\_QUERY,  
},  
});

**const Controlled = () =\> {**  
**const [value, setValue] = useState\<TimeframeV2 | null\>(null);**

**return \<TimeframeSelector value={value} onChange={setValue} /\>;**  
**};**

const columns: TableColumn[] = [  
{  
header: ‘Event Category’,

.

.

.

Thanks

---

<div class="post-metadata">

**Author:** ![chris\_cho](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/chris_cho/32/23527_2.png) [@chris\_cho](https://community.dynatrace.com/u/chris_cho)\
**Post date:** [January 20, 2025, 4:22am UTC](https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814/2 "2025-01-20T04:22:15Z")

</div>

Hello&nbsp;[@Tanveermd](https://community.dynatrace.com/user/75062)&nbsp;,

Before rendering the DataTable control,&nbsp; you may need to check _ **isLoading** _&nbsp;property from the _ **result** _.  
If _true_, show something that is loading the data, once is _false_, show the DataTable.  
You may also consider the examples related to DataTableV2 control.

---

<div class="post-metadata">

**Author:** ![imsingh](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/imsingh/32/7858_2.png) [@imsingh](https://community.dynatrace.com/u/imsingh)\
**Post date:** [January 21, 2025, 3:28pm UTC](https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814/3 "2025-01-21T15:28:15Z")

</div>

[@Tanveermd](https://community.dynatrace.com/user/75062)&nbsp;You have to pass the value from the timeframe selector to the useDqlQuery hook to make it work, as following:

```javascript
const queryResult = useDqlQuery({
    body: {
      query: "fetch logs",
      defaultTimeframeStart: value?.from.absoluteDate,
      defaultTimeframeEnd: value?.to.absoluteDate,
    }
});

```

Let me know it works  
Indermohan Singh

---

<div class="post-metadata">

**Author:** ![MostafaHussein](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mostafahussein/32/2785_2.png) [@MostafaHussein](https://community.dynatrace.com/u/MostafaHussein)\
**Post date:** [February 6, 2025, 11:07am UTC](https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814/4 "2025-02-06T11:07:29Z")

</div>

take in consideration if you’ve any dependent components like filtration bar form you’ll need to save its state before refetch your DQL Query when you change the timeframe value try the below

queries.ts

```javascript
export const PROBLEMS_QUERY = (timeframeFrom, timeframeTo) => {
    return `fetch ..problems.. , from:${timeframe}, to:${timeframeTo}`
}

```

ProblemList.tsx

```javascript
import React, { useState } from 'react';
import { Flex } from '@dynatrace/strato-components/layouts';
import { TitleBar } from '@dynatrace/strato-components-preview/layouts';
import { ProgressCircle } from '@dynatrace/strato-components/content';
import { convertToTimeseries } from '@dynatrace/strato-components-preview/conversion-utilities';
import { DataTable,TableColumn } from '@dynatrace/strato-components-preview/tables';
import { useDqlQuery } from '@dynatrace-sdk/react-hooks';
import { PROBLEMS_QUERY } from '../queries';
import { Colors } from '@dynatrace/strato-design-tokens';
import type { TimeframeV2 } from '@dynatrace/strato-components-preview/core';
import { TimeframeSelector } from '@dynatrace/strato-components-preview/forms';

export const ProblemsList = () => {
    const [timeframeValue, setTimeframeValue] = useState<TimeframeV2 | null>(null);
    const [filtrationValues, setFiltraionValues] = useState<object | null>(null);

    const queryResult = useDqlQuery({
        body: {
            query: PROBLEMS_QUERY(timeframeValue.from, timeframeValue.to)
    });

    const handleProblemFilterChange = (filterValues) => {
        setFilterValues(filterValues);
        // the rest of your filtration logic
    }

    useEffect(() => {
        if(filtrationValues !== null) {
            handleProblemFilterChange(filtrationValues);
        }
    }, [queryResult]);
    return (.. your react components ..)
});

```

without this, every time you change the timeframe values the other dependent components states will be reset to nulls _(e.g. problem state)_ and you’ve to re-select its old values again.

hope this helps,  
Mostafa Hussein.

---

<div class="post-metadata">

**Author:** ![zaid-bashir](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/zaid-bashir/32/22651_2.png) [@zaid-bashir](https://community.dynatrace.com/u/zaid-bashir)\
**Post date:** [February 10, 2025, 11:33am UTC](https://community.dynatrace.com/t/adding-timeframe-selector-in-customapp/49814/5 "2025-02-10T11:33:08Z")

</div>

Hi&nbsp;[@Tanveermd](https://community.dynatrace.com/user/75062)

I was also creating one app having same use case, i did it using the below simple steps.  
1) Create a hook for state variable as

```markup
const [value, setValue] = useState<TimeframeV2 | null>(null);

```

2) Use TimeFrame Component as below with **onChange prop** that will help you to get new selected timeframe object **and value prop** &nbsp; which is an object contains two sub objects&nbsp; **from** and **to.**

**=======================**  
from : value.from.value  
to: value.to.value

**=======================**

```markup
<TimeframeSelector style={{ alignItems: "right" }} value={value} onChange={setValue} />

```

3)Finally if you have to now give dynamic timeframe to dql query here is the query with null conditions resolved.

```markup
const fetchDistinctBizEvents = `fetch bizevents, from:{${value == null ? "now()-24h" : value?.from.value}}, to:{${value == null ? "now()" : value?.to.value}}

```

Thanks
