# Check the correct use of hooks

## useState

useState infers the type according to its initial state, if you don't define that state you can define the type manually, example:

```typescript
const [message, setMessage] = useProp<string>();
```

The above statement will define that:

1. `message` is of type `string`.
2. `setMessage` only accepts values of type `string` or functions that return a `string`.

## useProp

useProp will not infer the type from the prop, you must define it, example:

```typescript
const [message, setMessage] = useProp<string>("message");
```

Let's remember that useProp has a return api similar to useState, so the previous declaration will define that:

1. `message` is of type `string`.
2. `setMessage` only accepts values of type `string` or functions that return a `string`.

## useMemo and useCallback

both useMemo vs useCallback infer its type based on the callback that builds the memo state, but for certain conditions you may need to force the type return, example:

```typescript
const message = useMemo<string>(() => "i'am atomico!");
```

Although useMemo infers that its type is string, you can change the return rules via the first type parameter.

The above statement will define that:

1. `message` is of type `string`.

> This applies equally to useCallback.

## useEvent

useEvent allows defining the detail structure through the type parameter, example:

```typescript
const dispatch = useEvent<{ id: string }>("MyCustomEvent", { bubbles: true });

dispatch({ id: string });
```

The above statement will define that:

1. The dispatch callback expects an object of type `{id: string}` as a parameter.

## useRef

useRef allows to define through the type parameter the expected value of current in the reference.

```typescript
const refForm = useRef<HTMLFormElement>();
```

The above statement defines:

1. `refForm?.current` is of the type `HTMLFormElement`.

## useHost

useHost defines that current always exists, so the optional selector is not necessary, the type parameter of useHost allows to define the source Element, example:

```typescript
const host = useHost<HTMLElement>();
```

All hooks in Atomico have Type declarations, explore them when importing each hook as a documentation source.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://atomico.gitbook.io/doc/guides/atomico-with-typescript/check-the-correct-use-of-hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
