Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add documentation for useTarget hook/feature #1567

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/docs/src/routes/docs/hooks/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,59 @@ export default function Button(props) {
return <span>{props.text}</span>;
}
```


## useTarget

The `useTarget` hook returns a variable or runs a function based on the target

### Get variable based on target

```tsx
import { useTarget, useStore } from '@builder.io/mitosis';

export default function MyName() {
const state = useStore({
get name() {
const prefix = useTarget<string | number | boolean>({
default: 'Default str',
react: 123,
angular: true,
vue: 'v',
});
return prefix + 'foo';
}
});

return (
<div>{state.name}</div>
);
}
```

`default` target is the fallback if the correct target can't be found in the object you pass into `useTarget`.


### Run function based on target

```tsx
import { onMount, useTarget } from '@builder.io/mitosis';

export default function MyLogger() {
onMount(() => {
useTarget({
react: () => {
console.log('react');
},
qwik: () => {
console.log('qwik');
},
default: () => {
console.log('the rest');
},
});
});

return <span></span>;
}
```