Skip to content

Commit b3c7317

Browse files
author
Christopher J Baker
authored
update examples for v2 (#94)
1 parent 208d09b commit b3c7317

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

README.md

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,8 @@ Or, you can hire us for training, consulting, or development. [Set up a free con
2323
For basic usage, we will use this simple React component:
2424

2525
```js
26-
import React from "react"
27-
import * as ReactDOM from "react-dom/client"
28-
// When using React 16 and 17 import ReactDom with the commented statement below instead:
29-
// import ReactDom from "react-dom"
30-
31-
const Greeting = ({ name }) => {
32-
return <h1>Hello, {name}</h1>
26+
const Greeting = () => {
27+
return <h1>Hello, World!</h1>
3328
}
3429
```
3530

@@ -38,7 +33,7 @@ With our React component complete, all we have to do is call `reactToWebComponen
3833
```js
3934
import reactToWebComponent from "react-to-webcomponent"
4035

41-
const WebGreeting = reactToWebComponent(Greeting, React, ReactDOM)
36+
const WebGreeting = reactToWebComponent(Greeting)
4237

4338
customElements.define("web-greeting", WebGreeting)
4439
```
@@ -59,23 +54,18 @@ In the above case, the web-greeting custom element is not making use of the `nam
5954

6055
## Working with Attributes
6156

62-
By default, custom elements created by `reactToWebComponent` only
63-
pass properties to the underlying React component. To make attributes
64-
work, you must specify your component's properties with
65-
[PropTypes](https://reactjs.org/docs/typechecking-with-proptypes.html) as follows:
57+
By default, custom elements created by `reactToWebComponent` only pass properties to the underlying React component. To make attributes work, you must specify your component's props.
6658

6759
```js
68-
import React from "react"
69-
import PropTypes from "prop-types"
70-
import * as ReactDOM from "react-dom/client"
71-
7260
const Greeting = ({ name }) => {
73-
return <h1>Hello, {name}</h1>
61+
return <h1>Hello, {name}!</h1>
7462
}
7563

76-
Greeting.propTypes = {
77-
name: PropTypes.string.isRequired,
78-
}
64+
const WebGreeting = reactToWebComponent(Greeting, {
65+
props: {
66+
name: "string",
67+
},
68+
})
7969
```
8070

8171
Now `reactToWebComponent` will know to look for `name` attributes

docs/complete-example.md

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,38 @@ import { Button } from "@mui/material"
77
import { ThemeProvider, createTheme } from "@mui/material/styles"
88

99
interface GreetingProps {
10-
name: string
11-
description: string
12-
colorMode: "light" | "dark" | undefined
13-
buttonVariant: "contained" | "text" | "outlined" | undefined
10+
name: string
11+
description: string
12+
colorMode?: "light" | "dark" | undefined
13+
buttonVariant?: "contained" | "text" | "outlined" | undefined
1414
}
1515

1616
export const Greeting = ({ name, description, colorMode = "light", buttonVariant = "text" }: GreetingProps) => {
17-
const themeMode = createTheme({
18-
palette: {
19-
mode: colorMode,
20-
},
21-
})
17+
const themeMode = createTheme({
18+
palette: {
19+
mode: colorMode,
20+
},
21+
})
2222

23-
return (
24-
<ThemeProvider theme={themeMode}>
25-
<main>
26-
<h1>Hello, {name}</h1>
27-
<p>{description}</p>
28-
<Button variant={buttonVariant}>This is the button</Button>
29-
</main>
30-
</ThemeProvider>
31-
)
23+
return (
24+
<ThemeProvider theme={themeMode}>
25+
<main>
26+
<h1>Hello, {name}</h1>
27+
<p>{description}</p>
28+
<Button variant={buttonVariant}>This is the button</Button>
29+
</main>
30+
</ThemeProvider>
31+
)
3232
}
3333

34-
const WebGreeting = reactToWebComponent(Greeting, React, ReactDOM)
34+
const WebGreeting = reactToWebComponent(Greeting, {
35+
props: {
36+
name: "string",
37+
description: "string",
38+
colorMode: "string",
39+
buttonVariant: "string",
40+
}
41+
})
3542

3643
customElements.define("web-greeting", WebGreeting)
3744
```

0 commit comments

Comments
 (0)