Skip to content

Instantly share code, notes, and snippets.

@tybro0103
Last active July 17, 2017 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tybro0103/6c0da722300a273463ea39f65746bc94 to your computer and use it in GitHub Desktop.
Save tybro0103/6c0da722300a273463ea39f65746bc94 to your computer and use it in GitHub Desktop.
Simple Isomorphic Bits
const isoComps = {
'session-menu-root': SessionMenu,
};
const isoData = window.isoData || {};
Object.keys(isoData).forEach(id => {
const props = isoData[id];
const el = document.getElementById(id);
const Comp = isoComps[id];
if (props && el && Comp) {
render(<Comp {...props} />, el);
}
});
class SessionMenu extends Component {
constructor(props) {
super(props);
this.state = {
showDropdown: false,
};
}
componentDidMount() {
'client stuff';
}
render() {
const { showDropdown } = this.state;
return (
<div>
<div onClick={() => {this.setState({showDropdown: !showDropdown})}}>
rendered on server and client
</div>
<div className={classNames({active: showDropdown})}>
triggered by client events
</div>
</div>
);
}
}
class MainLayout extends Component {
getIsoData() {
return {
'session-menu-root': {
currentUser: this.props.currentUser,
},
};
}
renderIsoComp(id, Component) {
const data = this.getIsoData()[id];
return <div id={id}><Component {...data} /></div>;
}
render() {
const isoData = this.getIsoData();
const isoDataJs = `window.isoData = ${JSON.stringify(isoData)};`;
return (
<Html>
<div className="header">{/*...*/}</div>
<div className="main">
<Whatevs />
{this.renderIsoComp('session-menu-root', SessionMenu)}
</div>
<div className="footer">{/*...*/}</div>
<script dangerouslySetInnerHTML={{ __html: isoDataJs }} />
</Html>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment