Compare commits
1 Commits
master
...
2b778a74e7
Author | SHA1 | Date |
---|---|---|
Evan Chen | 2b778a74e7 |
|
@ -9,11 +9,11 @@
|
|||
content="Web site created using create-react-app"
|
||||
/>
|
||||
<title>React App</title>
|
||||
<link rel="stylesheet" href="index.css">
|
||||
<link rel="stylesheet" href="dist/index.css">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<wc-test id='test' name='Evan'></wc-test>
|
||||
<wc-test></wc-test>
|
||||
</body>
|
||||
<script src="index.js" type="module"></script>
|
||||
<script src="dist/index.js"></script>
|
||||
</html>
|
||||
|
|
49
index.js
49
index.js
|
@ -4,58 +4,35 @@ import ReactDOM from 'react-dom/client';
|
|||
|
||||
import logo from './logo.svg';
|
||||
|
||||
const WcTestApp = (props) => {
|
||||
const WcTestApp = () => {
|
||||
const [ctr,incr] = useState(0)
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<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)}}/>
|
||||
</header>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
WcTestApp.defaultProps = {
|
||||
name: 'World'
|
||||
};
|
||||
|
||||
// a wrapper class for react component
|
||||
class WcTest extends HTMLElement {
|
||||
root;
|
||||
|
||||
constructor(){
|
||||
super()
|
||||
let mountPoint = this;
|
||||
connectedCallback() {
|
||||
const 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)
|
||||
// mountPoint = this.attachShadow({ mode: 'open' });
|
||||
// this.attachShadow({ mode: 'open' }).appendChild(mountPoint);
|
||||
|
||||
this.root = ReactDOM.createRoot(mountPoint);
|
||||
this.render()
|
||||
const root = ReactDOM.createRoot(mountPoint);
|
||||
|
||||
// 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);
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "prime-react-webcomponent",
|
||||
"name": "wc-test",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
|
@ -10,8 +10,8 @@
|
|||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "yarn parcel index.html",
|
||||
"build": "(rm -rf dist||true) && yarn parcel build index.html"
|
||||
"start": "yarn parcel index.js index.css",
|
||||
"build": "yarn parcel build index.js index.css"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
|
|
Loading…
Reference in New Issue