Skip to content

Commit 4b98433

Browse files
Export internal hooks for public usage (#15)
* Update index.tsx * Update index.tsx * refactor: clean up types * feat: export useDittoComponent * refactor: further type cleanup * feat/refactor: further type fixes, update example code to include hook usage * chore: update README to detail hooks, fix misc typos * fix: straggling type error Co-authored-by: Jordin Gardner <[email protected]>
1 parent a736c2b commit 4b98433

File tree

11 files changed

+372
-281
lines changed

11 files changed

+372
-281
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ const App = () => {
9292

9393
## Interpolation
9494

95-
Support dynamic text by passing in variables into your text:
95+
Support dynamic text by specifying variables:
9696

9797
```jsx
9898
<Ditto textId={textId} />
99-
// The cart contains {{itemCount}} {{itemName}}
100-
<Ditto textId={textId} variables={{ itemName: "apples", itemCount: 10 }}/>
101-
// The cart contains 10 apples
102-
<Ditto textId={textId} variables={{ itemName: "pears", itemCount: 6 }}/>
103-
// The cart contains 6 pears
99+
// The cart contains {{itemName}}
100+
<Ditto textId={textId} variables={{ itemName: "apples" }}/>
101+
// The cart contains apples
102+
<Ditto textId={textId} variables={{ itemName: "pears" }}/>
103+
// The cart contains pears
104104
```
105105

106106
Learn how to configure Ditto variables: https://www.dittowords.com/docs/variables
@@ -112,7 +112,7 @@ Ditto pluralization can be utilized by passing in the `count` prop:
112112
```jsx
113113
<Ditto textId={textId}/> // The cart contains {{numItems}} items
114114
<Ditto textId={textId} variables={{ numItems: 3 }} count={3}/> // The cart contains 3 items
115-
<Ditto textId={textId} variables={{ numItems: 1 }}count={1}/> // The cart contains 1 item
115+
<Ditto textId={textId} variables={{ numItems: 1 }} count={1}/> // The cart contains 1 item
116116
<Ditto textId={textId} variables={{ numItems: 0 }} count={0}/> // The cart contains no items
117117
```
118118

@@ -224,7 +224,7 @@ If you pass `frameId` and/or `blockId`, the specified frame/block object will be
224224
</Ditto>
225225
```
226226

227-
### Note
227+
### Type-specific component exports
228228

229229
In addition to the `<Ditto />` component, individual exports of each specific component type are also available. These behave identically to passing the respective prop configurations to the `<Ditto />` component, but may provide a better experience for TypeScript users due to their improved out-of-the-box type safety and inference:
230230

@@ -237,6 +237,19 @@ import {
237237
} from "ditto-react";
238238
```
239239

240+
### Hooks (experimental)
241+
242+
Hooks are also available for consuming data from a `<DittoProvider />`. The two hooks currently exposed have unpolished ergonomics due to the manner in which they evolved from legacy constraints.
243+
244+
In future versions of `ditto-react`, a hooks-based API will be the primary way through which data is accessed (instead of components), and the API will likely have some differences from what is currently available.
245+
246+
```js
247+
import { useDittoComponent, useDittoSingleText } from "ditto-react";
248+
249+
const componentText = useDittoComponent({ componentId: "xxx-xxx" });
250+
const text = useDittoSingleText({ textId: "xxx-xxx" }); // assumes `projectId` was specified for an ancestor <DittoProvider />
251+
```
252+
240253
## Additional Examples
241254

242255
### Example: Single Text

0 commit comments

Comments
 (0)