Skip to content

Commit c1c44d6

Browse files
committedApr 12, 2023
docs done
1 parent 6c9ecb3 commit c1c44d6

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed
 

‎docs/pages/docs/usage/hooks.mdx

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import Callout from 'nextra-theme-docs';
2+
13
# Using React Hooks
24

35
Sveltris allows you to use React hooks inside your Svelte component by wrapping the call inside `use`.
46

57
`use` returns a `readonly` svelte store that can be subscribed to by your component.
68

7-
The value of this store is `undefined` during the initial render, so it needs to be accessed inside an `#if` block.
9+
<Callout>
10+
The value of this store is `undefined` during the initial render, so it needs
11+
to be accessed inside an `#if` block.
12+
</Callout>
813

914
```html
1015
<script>
@@ -24,4 +29,4 @@ const counter = use(useState(0))
2429

2530
## Limitations
2631

27-
It has some limitations, for eg. you can't use hooks that depend on a parent `ContextProvider` being there in the react root. But if the `ContextProvider` isn't stateful then it can be passed as the second argument of `use`
32+
It has some limitations, for eg. you can't use hooks that depend on a parent `ContextProvider` being there in the react root. But if the `ContextProvider` isn't stateful then it can be passed as the second argument of `use`

‎docs/pages/docs/usage/react-in-svelte.mdx

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Callout from 'nextra-theme-docs';
2+
13
# React in Svelte
24

35
With Sveltris, you can use any React component inside Svelte as if it was a regular Svelte component.
@@ -10,7 +12,18 @@ With Sveltris, you can use any React component inside Svelte as if it was a regu
1012
<Counter />
1113
```
1214

13-
Sveltris also maps all React and custom event handlers (`/on(.*)/`) to Svelte format, and you can set them using `/on:(.*)/` syntax.
15+
<Callout>
16+
The React component must be a default export for it to work. If it is not a default export, then you can create another `.js` file that re-exports the named export as a default export.
17+
18+
```js
19+
export { Counter as default } from './Counter?in-svelte';
20+
```
21+
22+
</Callout>
23+
24+
## Props and Events
25+
26+
Sveltris also accepts all props, and triggers a selective rerender whenever the props update. It also maps all React and custom event handlers (`/on(.*)/`) to Svelte format, and you can set them using `/on:(.*)/` syntax.
1427
So if a component has an `onSubmit` prop, you can set it as `on:submit`
1528

1629
```html
@@ -22,5 +35,3 @@ So if a component has an `onSubmit` prop, you can set it as `on:submit`
2235

2336
<Input {value} on:change={v => value = v} />
2437
```
25-
26-
These components also _just work_ with SSR without any extra configuration required.
+39
Original file line numberDiff line numberDiff line change
@@ -1 +1,40 @@
11
# Svelte in React
2+
3+
With Sveltris, you can use any Svelte component inside React as if it was a regular React component.
4+
5+
```jsx
6+
import Counter from './Counter.svelte?in-react';
7+
8+
<Counter />;
9+
```
10+
11+
## Props and Events
12+
13+
You can also pass `props` and events to this component, just like in a regular React component. Updates to them automatically triggers a rerender of the Svelte component.
14+
15+
All svelte events such as `on:click` are mapped to camelCase like `onClick`.
16+
17+
```jsx
18+
import Input from './Input.svelte?in-react';
19+
import { useState } from 'react'
20+
21+
const [value, setValue] = useState("")
22+
23+
<Input value={value} onChange={v => setValue(v)} />;
24+
```
25+
26+
## Slots and children
27+
28+
`children` passed to these components is set as the default slot of the underlying Svelte component
29+
30+
```jsx
31+
import Layout from './Layout.svelte?in-react';
32+
33+
<Layout>
34+
<h1>Hello world</h1>
35+
</Layout>;
36+
```
37+
38+
## SSR
39+
40+
These components also _just work_ with SSR without any extra configuration required.

0 commit comments

Comments
 (0)
Please sign in to comment.