update
parent
cc27b07ce6
commit
f1698b4cb2
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<wc-test></wc-test>
|
<wc-test id='test' name='Evan'></wc-test>
|
||||||
</body>
|
</body>
|
||||||
<script src="dist/index.js"></script>
|
<script src="dist/index.js"></script>
|
||||||
</html>
|
</html>
|
||||||
|
|
46
index.js
46
index.js
|
@ -4,35 +4,57 @@ import ReactDOM from 'react-dom/client';
|
||||||
|
|
||||||
import logo from './logo.svg';
|
import logo from './logo.svg';
|
||||||
|
|
||||||
const WcTestApp = () => {
|
const WcTestApp = (props) => {
|
||||||
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>
|
<p>Hello! {props.name}.</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;
|
||||||
|
|
||||||
|
// declare attributes we want to keep track of
|
||||||
|
static get observedAttributes() {
|
||||||
|
return ['name', 'onclick'];
|
||||||
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const mountPoint = this;
|
let mountPoint = this;
|
||||||
// to get attributes from outside
|
|
||||||
// const name = this.getAttribute('name');
|
|
||||||
|
|
||||||
// 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)
|
||||||
// this.attachShadow({ mode: 'open' }).appendChild(mountPoint);
|
// mountPoint = this.attachShadow({ mode: 'open' });
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(mountPoint);
|
this.root = ReactDOM.createRoot(mountPoint);
|
||||||
|
this.render()
|
||||||
// render
|
|
||||||
root.render(<WcTestApp />);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
render(){
|
||||||
|
|
||||||
|
const name = this.hasAttribute('name')
|
||||||
|
? this.getAttribute('name')
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
console.log(this.root)
|
||||||
|
|
||||||
|
this.root.render(<WcTestApp name={name} />);
|
||||||
|
}
|
||||||
|
|
||||||
|
attributeChangedCallback(){
|
||||||
|
this.render()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
customElements.define('wc-test', WcTest);
|
customElements.define('wc-test', WcTest);
|
Loading…
Reference in New Issue