Compare commits
3 Commits
2b778a74e7
...
master
Author | SHA1 | Date |
---|---|---|
Evan Chen | 6b63b9f8c6 | |
Evan Chen | f1698b4cb2 | |
Evan Chen | cc27b07ce6 |
|
@ -9,11 +9,11 @@
|
|||
content="Web site created using create-react-app"
|
||||
/>
|
||||
<title>React App</title>
|
||||
<link rel="stylesheet" href="dist/index.css">
|
||||
<link rel="stylesheet" href="index.css">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<wc-test></wc-test>
|
||||
<wc-test id='test' name='Evan'></wc-test>
|
||||
</body>
|
||||
<script src="dist/index.js"></script>
|
||||
<script src="index.js" type="module"></script>
|
||||
</html>
|
||||
|
|
49
index.js
49
index.js
|
@ -4,35 +4,58 @@ import ReactDOM from 'react-dom/client';
|
|||
|
||||
import logo from './logo.svg';
|
||||
|
||||
const WcTestApp = () => {
|
||||
const WcTestApp = (props) => {
|
||||
const [ctr,incr] = useState(0)
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
<header className="App-header">
|
||||
<img src={logo} className="App-logo" alt="logo" />
|
||||
<p>
|
||||
Edit <code>src/App.js</code> and save to reload.
|
||||
</p>
|
||||
<p>Hello! {props.name}.</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 {
|
||||
connectedCallback() {
|
||||
const mountPoint = this;
|
||||
// to get attributes from outside
|
||||
// const name = this.getAttribute('name');
|
||||
root;
|
||||
|
||||
constructor(){
|
||||
super()
|
||||
let mountPoint = this;
|
||||
|
||||
// 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);
|
||||
|
||||
// render
|
||||
root.render(<WcTestApp />);
|
||||
this.root = ReactDOM.createRoot(mountPoint);
|
||||
this.render()
|
||||
}
|
||||
|
||||
// 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": "wc-test",
|
||||
"name": "prime-react-webcomponent",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
|
@ -10,8 +10,8 @@
|
|||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "yarn parcel index.js index.css",
|
||||
"build": "yarn parcel build index.js index.css"
|
||||
"start": "yarn parcel index.html",
|
||||
"build": "(rm -rf dist||true) && yarn parcel build index.html"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
|
|
Loading…
Reference in New Issue