This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.ReactCounter = { | |
mount: (props, container) => { | |
ReactDOM.render(<Counter {...props} />, container); | |
}, | |
unmount: (container) => { | |
ReactDOM.unmountComponentAtNode(container); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.ReactCounter = { | |
mount: (props) => { | |
const el = document.getElementById('counter-app'); | |
ReactDOM.render(<Counter {...props} />, el); | |
}, | |
unmount: () => { | |
const el = document.getElementById('counter-app'); | |
ReactDOM.unmountComponentAtNode(el); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Counter extends Component { | |
state = { count: this.props.initialCount || 0 } | |
updateCount = val => () => { | |
const { onCountUpdate } = this.props; | |
this.setState(state => { | |
const nextCount = state.count + val; | |
return { count: nextCount } | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
componentDidUpdate(prevProps, prevState) { | |
const { showCounter } = this.state; | |
if (prevState.showCounter !== showCounter) { | |
if(showCounter){ | |
window.ReactCounter.mount({title: 'Whaaa! cool'}); | |
} else{ | |
window.ReactCounter.unmount(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App extends Component { | |
render() { | |
return ( | |
<div className="App"> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<div id="counter-app"></div> | |
<p> | |
This is the main App. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<meta name="theme-color" content="#000000"> | |
<link rel="manifest" href="%PUBLIC_URL%/manifest.json"> | |
<title>React App</title> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {PureComponent} from 'react'; | |
class ReactCounter extends PureComponent { | |
// create a ref so we can pass the element to mount and unmount | |
counterRef = React.createRef(); | |
componentDidMount() { | |
// initial render with props | |
window.ReactCounter.mount(this.props, this.counterRef.current); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function typeCheck(obj, definition) { | |
return new Proxy(obj, { | |
set(obj, key, value) { | |
if (key in definition && typeof value !== definition[key]) { | |
throw new TypeError(`Invalid type of ${key} '${typeof value}'. expected to be '${definition[key]}'`) | |
} | |
return Reflect.set(obj, key, value); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createStore(reducer, initialState) { | |
var currentReducer = reducer; | |
var currentState = initialState; | |
var listeners = []; | |
var isDispatching = false; | |
function getState() { | |
return currentState; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// with class pattern | |
class Client { | |
constructor(name) { | |
this.name = name; | |
this.mediator = null; | |
} | |
send(message, to) { | |
if (!this.mediator) { | |
throw ('you need to register to a mediator first') |