Skip to content

Commit 974753a

Browse files
authored
Update api.md
1 parent 2707040 commit 974753a

File tree

1 file changed

+4
-83
lines changed

1 file changed

+4
-83
lines changed

docs/api.md

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function Greeting({ camelCaseName }) {
9595
customElements.define(
9696
"my-dashed-style-greeting",
9797
reactToWebComponent(Greeting, {
98-
props: ["camelCaseName"],
98+
props: { camelCaseName: "string" },
9999
}),
100100
)
101101

@@ -107,9 +107,10 @@ console.log(document.body.firstElementChild.innerHTML) // "<h1>Hello, Jane</h1>"
107107

108108
## Typed Props
109109

110-
If `options.props` is an object, the keys are the camelCased React props and the values are any one of the following built in javascript types, or the string "ref":
110+
If `options.props` is an object, the keys are the camelCased React props and the values are any one of the following built in javascript types.
111+
This is the recommended way of passing props to r2wc.
111112

112-
`"string" | "number" | "boolean" | "function" | "json" | "ref"`
113+
`"string" | "number" | "boolean" | "function" | "json"`
113114

114115
"json" can be an array or object. The string passed into the attribute must pass `JSON.parse()` requirements.
115116

@@ -200,83 +201,3 @@ setTimeout(
200201
)
201202
// ^ calls globalFn, logs: true, "Jane"
202203
```
203-
204-
### "ref" props
205-
206-
If the React component is can provide a ref to itself or has ref props, you can specify attributes as `"ref"` type.
207-
208-
For example, given this class component:
209-
210-
```js
211-
class ComRef extends React.Component {
212-
constructor(props) {
213-
super(props)
214-
this.exposedToParentByRef = true
215-
this.h1Ref = props.h1Ref
216-
}
217-
render() {
218-
return <h1 ref={this.props.h1Ref}>Ref</h1>
219-
}
220-
}
221-
```
222-
223-
or given this functional component:
224-
225-
```js
226-
/* note useImperativeHandle is not available in preact, but class component `ref` will work as expected in both preact and react */
227-
228-
const ComRef = React.forwardRef(function ComRef(props, ref) {
229-
// set up a reference obj to the component itself
230-
React.useImperativeHandle(ref, () => ({
231-
exposedToParentByRef: true,
232-
h1Ref: props.h1Ref,
233-
}))
234-
return <h1 ref={props.h1Ref}>Ref</h1>
235-
})
236-
```
237-
238-
Then `React.createRef()` will automatically happen behind the scenes then attach the reference to the webcomponent instance if it has the corresponding attribute.
239-
240-
```js
241-
class WebComRef extends reactToWebComponent(ComRef, {
242-
props: {
243-
ref: "ref",
244-
h1Ref: "ref",
245-
},
246-
}) {}
247-
248-
customElements.define("ref-example", WebComRef)
249-
250-
// add "ref" and "h1-ref" attrs to opt-in to having them attached to the webcomponent instance:
251-
document.body.innerHTML = "<ref-example ref h1-ref></ref-example>"
252-
253-
setTimeout(() => {
254-
const el = document.querySelector("ref-example")
255-
256-
console.log(el.ref.current.exposedToParentByRef) // logs true using either the functional or class example above
257-
console.log(el.ref.current instanceof ComRef) // logs true only if you used the class ComRef component example
258-
259-
const h1 = el.querySelector("h1")
260-
261-
console.log(el.h1Ref.current === h1) // logs true
262-
}, 0)
263-
```
264-
265-
#### Specifing a callback function for ref props
266-
267-
If your `"ref"` type webcomponent attribute specifies a value, the value will be the name of a global function (like the `Function` prop type above) and be used as a callback reference, recieving the dom element the React component attaches it to as a parameter.
268-
269-
```js
270-
window.globalRefFn = function (el) {
271-
if (!el) {
272-
// if the component rerenders the referenced element, the callback may run with el = null
273-
return
274-
}
275-
console.log(el === this.querySelector("h1")) // logs true
276-
}
277-
278-
// opt-in to the h1-ref attr (h1Ref prop) and specify callback function to use:
279-
body.innerHTML = "<ref-example h1-ref='globalRefFn'></ref-example>"
280-
```
281-
282-
Note: React only supports callback references to elements, not to component instances

0 commit comments

Comments
 (0)