Skip to content

Commit b874a0e

Browse files
committed
✨ Doc
1 parent 96e5a87 commit b874a0e

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

README.md

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ Now we can use `<web-greeting>` like any other HTML element!
3030
</body>
3131
```
3232

33-
Note that by using React 18, `r2wc` will use the new root API. If your application needs the legacy API, please use React 17
34-
35-
In the above case, the web-greeting custom element is not making use of the `name` property from our `Greeting` component.
36-
3733
## Working with Attributes
3834

3935
By default, custom elements created by `r2wc` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
@@ -61,6 +57,95 @@ as follows:
6157
</body>
6258
```
6359

60+
## Working with complex attributes and functions callback
61+
62+
#### React component
63+
64+
```jsx
65+
const component = r2wc(App, {
66+
props: {
67+
onCountUpdated: "function",
68+
title: "string",
69+
complex: "json",
70+
},
71+
});
72+
customElements.define("app-component", component);
73+
```
74+
75+
#### React consumer
76+
77+
```jsx
78+
function App() {
79+
useScript("http://localhost:3000/index.es.js");
80+
81+
const [count, setCount] = useState(0);
82+
const func = (count: number) => {
83+
setCount(count);
84+
};
85+
const [title, setTitle] = useState("");
86+
const [complex, setComplex] = useState({
87+
name: "",
88+
value: "",
89+
});
90+
91+
useEffect(() => {
92+
setTitle(count % 2 === 0 ? "Foo" : "Bar");
93+
setComplex({
94+
name: `Counter is: ${count}`,
95+
value: `Value: ${count}`,
96+
});
97+
}, [count]);
98+
99+
return (
100+
<>
101+
<h2>Consumer</h2>
102+
<app-component
103+
onCountUpdated={func}
104+
title={title}
105+
complex={complex}
106+
></app-component>
107+
</>
108+
);
109+
}
110+
```
111+
112+
#### Vanilla js
113+
114+
```html
115+
<body>
116+
<div id="root">
117+
<h2>Consumer</h2>
118+
<app-component
119+
id="app"
120+
on-count-updated="onUpdated"
121+
title=""
122+
complex="{}"
123+
></app-component>
124+
</div>
125+
126+
<script>
127+
window.onload = () => {
128+
onUpdated(0);
129+
};
130+
131+
const app = document.getElementById("app");
132+
133+
function onUpdated(count) {
134+
console.log(count);
135+
136+
app.props = {
137+
title: count % 2 === 0 ? "Foo" : "Bar",
138+
complex: {
139+
name: `Counter is: ${count}`,
140+
value: `Value: ${count}`,
141+
},
142+
};
143+
}
144+
</script>
145+
<script src="http://localhost:3000/index.es.js"></script>
146+
</body>
147+
```
148+
64149
## Setup 🔨
65150

66151
To install from npm:
@@ -80,3 +165,7 @@ Based on https://github.com/bitovi/react-to-web-component
80165
## License ⚖️
81166

82167
[MIT](https://choosealicense.com/licenses/mit/)
168+
169+
```
170+
171+
```

0 commit comments

Comments
 (0)