# TimeframeSelector with useDqlQuery()

**URL:** <https://community.dynatrace.com/t/timeframeselector-with-usedqlquery/46771>\
**Category:** Developer Q&A Forum\
**Tags:** custom-app\
**Created:** [July 23, 2024, 1:39pm UTC](https://community.dynatrace.com/t/timeframeselector-with-usedqlquery/46771 "2024-07-23T13:39:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jegron](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/jegron/32/12797_2.png) [@jegron](https://community.dynatrace.com/u/jegron)\
**Post date:** [July 23, 2024, 1:39pm UTC](https://community.dynatrace.com/t/timeframeselector-with-usedqlquery/46771/1 "2024-07-23T13:39:05Z")

</div>

Hi,

How can I use TimeframeSelector with my DQL requests ?

- [https://developer.dynatrace.com/reference/design-system/preview/forms/TimeframeSelector/](https://developer.dynatrace.com/reference/design-system/preview/forms/TimeframeSelector/)

My initial value is set correctly :

```markup
const [value, setValue] = useState<TimeframeSelectorProps['value']>({
    from: 'now()-14d',
    to: 'now()',
  });

const CLUSTER = `fetch logs, from:'${value.from}'
| filter contains(log.source, "Dynatrace Automation Script")
| makeTimeseries count(), by:{content.cluster}, interval:30m`;

const cluster_result = useDqlQuery({
    body: {
      query: CLUSTER,
    },
  });

```

But I failed to use it in my DQL because of :

```markup
'value' is possibly 'null' or 'undefined'.ts(18049)
const value: RequireAtLeastOne<{
    from: string;
    to: string;
}> | RequireAtLeastOne<TimeframeV2> | null | undefined

```

Do you have an example to help me ?

Julien

---

<div class="post-metadata">

**Author:** ![educampver](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/educampver/32/29917_2.png) [@educampver](https://community.dynatrace.com/u/educampver)\
**Post date:** [July 24, 2024, 7:57am UTC](https://community.dynatrace.com/t/timeframeselector-with-usedqlquery/46771/2 "2024-07-24T07:57:17Z")

</div>

Hi&nbsp;[@jegron](https://community.dynatrace.com/user/47129), the type&nbsp;`TimeframeSelectorProps['value']` is nullable, and the type checker can’t infer that `value` is actually defined. You have two options:

1. Use the non-null assertion operator `!` like this `value!.from`. This is usually not recommended and will likely lead to a `Forbidden non-null assertion` warning.
2. Use the optional chaining `?` and nullish&nbsp;coalescing `??` operators, which allow you&nbsp;to fall back to a default value when dealing with `null` or `undefined`. Here’s how to use them:

```javascript
const DEFAULT_FROM = 'now()-14d';

const [value, setValue] = useState<TimeframeSelectorProps['value']>({
  from: DEFAULT_FROM,
  to: 'now()',
});

const CLUSTER = `fetch logs, from:'${value?.from ?? DEFAULT_FROM}'
| filter contains(log.source, "Dynatrace Automation Script")
| makeTimeseries count(), by:{content.cluster}, interval:30m`;

```

---

<div class="post-metadata">

**Author:** ![jegron](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/jegron/32/12797_2.png) [@jegron](https://community.dynatrace.com/u/jegron)\
**Post date:** [July 25, 2024, 2:25pm UTC](https://community.dynatrace.com/t/timeframeselector-with-usedqlquery/46771/3 "2024-07-25T14:25:47Z")

</div>

Thanks!

My timeframe are now correctly update onChange :

```javascript
<TimeframeSelector value={value} onChange={newValue => {
          console.log('New timeframe selected:', newValue);
          setValue(newValue);
        }} clearable/>

```

![jegron_0-1721917247590.png](https://global.discourse-cdn.com/dynatrace/original/2X/1/100df7087c87beb3ef9aaa5817a4c841786711dd.png)

But my “useDQLQuery()” requests/results are not refetch. What is the best way to do it ?

---

<div class="post-metadata">

**Author:** ![StefanKern](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@StefanKern](https://community.dynatrace.com/u/StefanKern)\
**Post date:** [August 2, 2024, 6:32am UTC](https://community.dynatrace.com/t/timeframeselector-with-usedqlquery/46771/4 "2024-08-02T06:32:56Z")

</div>

Hello&nbsp;[@jegron](https://community.dynatrace.com/user/47129), the useDqlQuery hook returns a refetch function, which you could use in your onChange to refetch fresh grail data. Here is a code example on the [Dynatrace Developer Portal](https://developer.dynatrace.com/develop/sdks/react-hooks/#simplified-data-querying-from-grail).
