JSFiddle - React, Tailwind, and code Playground
by Andre Lee
HTML
<script src="http://www.andretw.com/[email protected]/dist/preact.js"></script>
<script src="http://andretw.com/[email protected]/dist/preact.js"></script>
Babel + JSX
/** @jsx h */
const { h, render, Component } = preact;
var notify;
var selection = 0;
class Menu extends Component {
switch(n) {
selection = n;
if (notify) notify();
}
render() {
return <SubMenu>
<button onClick={() => this.switch(0)}>Switch to 0</button>
<button onClick={() => this.switch(1)}>Switch to 1</button>
</SubMenu>
}
}
class SubMenu extends Component {
const children = this.props.children
render() {
return <div>
{...children}
</div>
}
}
class Switcher extends Component {
constructor () {
super();
notify = () => {
this.setState({selection: selection});
};
this.state = {
selection: 0
}
}
render() {
return this.props.children[this.state.selection];
}
}
class Comp0 extends Component {
render() {
return <div>I'm a div</div>;
}
}
class Comp1 extends Component {
constructor(props) {
super(props);
this.state = {switch: false};
setTimeout(() => this.setState({switch:true}), 100);
}
render() {
if (this.state.switch) {
return <span>I'm a span</span>;
}
return "I'm a text";
}
}
render(
<div>
<Menu />
<br />
Selected Component:
<Switcher>
<Comp0 />
<Comp1 />
</Switcher>
</div>, document.body);