Compare commits
1 Commits
master
...
2b778a74e7
Author | SHA1 | Date |
---|---|---|
Evan Chen | 2b778a74e7 |
|
@ -9,11 +9,11 @@
|
||||||
content="Web site created using create-react-app"
|
content="Web site created using create-react-app"
|
||||||
/>
|
/>
|
||||||
<title>React App</title>
|
<title>React App</title>
|
||||||
<link rel="stylesheet" href="index.css">
|
<link rel="stylesheet" href="dist/index.css">
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<wc-test id='test' name='Evan'></wc-test>
|
<wc-test></wc-test>
|
||||||
</body>
|
</body>
|
||||||
<script src="index.js" type="module"></script>
|
<script src="dist/index.js"></script>
|
||||||
</html>
|
</html>
|
||||||
|
|
49
index.js
49
index.js
|
@ -4,58 +4,35 @@ import ReactDOM from 'react-dom/client';
|
||||||
|
|
||||||
import logo from './logo.svg';
|
import logo from './logo.svg';
|
||||||
|
|
||||||
const WcTestApp = (props) => {
|
const WcTestApp = () => {
|
||||||
const [ctr,incr] = useState(0)
|
const [ctr,incr] = useState(0)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<header className="App-header">
|
<header className="App-header">
|
||||||
<img src={logo} className="App-logo" alt="logo" />
|
<img src={logo} className="App-logo" alt="logo" />
|
||||||
<p>Hello! {props.name}.</p>
|
<p>
|
||||||
|
Edit <code>src/App.js</code> and save to reload.
|
||||||
|
</p>
|
||||||
<Button label={`clicked ${ctr} times`} icon="pi pi-check" onClick={()=>{incr(ctr+1)}}/>
|
<Button label={`clicked ${ctr} times`} icon="pi pi-check" onClick={()=>{incr(ctr+1)}}/>
|
||||||
</header>
|
</header>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
WcTestApp.defaultProps = {
|
|
||||||
name: 'World'
|
|
||||||
};
|
|
||||||
|
|
||||||
// a wrapper class for react component
|
|
||||||
class WcTest extends HTMLElement {
|
class WcTest extends HTMLElement {
|
||||||
root;
|
connectedCallback() {
|
||||||
|
const mountPoint = this;
|
||||||
constructor(){
|
// to get attributes from outside
|
||||||
super()
|
// const name = this.getAttribute('name');
|
||||||
let mountPoint = this;
|
|
||||||
|
|
||||||
// ShadowRoot (global styles will not apply, but safer with a complete isolation layer)
|
// ShadowRoot (global styles will not apply, but safer with a complete isolation layer)
|
||||||
// mountPoint = this.attachShadow({ mode: 'open' });
|
// this.attachShadow({ mode: 'open' }).appendChild(mountPoint);
|
||||||
|
|
||||||
this.root = ReactDOM.createRoot(mountPoint);
|
const root = ReactDOM.createRoot(mountPoint);
|
||||||
this.render()
|
|
||||||
|
// render
|
||||||
|
root.render(<WcTestApp />);
|
||||||
}
|
}
|
||||||
|
|
||||||
// declare attributes we want to keep track of
|
|
||||||
static get observedAttributes() {
|
|
||||||
return ['name', 'onclick'];
|
|
||||||
}
|
|
||||||
|
|
||||||
// connectedCallback() {
|
|
||||||
// }
|
|
||||||
|
|
||||||
render(){
|
|
||||||
const name = this.hasAttribute('name')
|
|
||||||
? this.getAttribute('name')
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
this.root.render(<WcTestApp name={name} />);
|
|
||||||
}
|
|
||||||
|
|
||||||
attributeChangedCallback(){
|
|
||||||
this.render()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
customElements.define('wc-test', WcTest);
|
customElements.define('wc-test', WcTest);
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "prime-react-webcomponent",
|
"name": "wc-test",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -10,8 +10,8 @@
|
||||||
"react-dom": "^18.2.0"
|
"react-dom": "^18.2.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "yarn parcel index.html",
|
"start": "yarn parcel index.js index.css",
|
||||||
"build": "(rm -rf dist||true) && yarn parcel build index.html"
|
"build": "yarn parcel build index.js index.css"
|
||||||
},
|
},
|
||||||
"browserslist": {
|
"browserslist": {
|
||||||
"production": [
|
"production": [
|
||||||
|
|
Loading…
Reference in New Issue