Skip to content

Instantly share code, notes, and snippets.

View sag1v's full-sized avatar
🎯
Focusing

Sagiv ben giat sag1v

🎯
Focusing
View GitHub Profile
class ReactRoot extends React.Component {
state = {
millis: Date.now(),
timezoneOffset: new Date().getTimezoneOffset(),
request: 0
};
componentDidMount() {
this.setState({
request: requestAnimationFrame(this.tick)
@sag1v
sag1v / reflowLogger.js
Created September 29, 2018 00:56
Just a logger for page layouts and reflows
/* these methods and props are known to cause reflow and layout
see list from https://gist.github.com/paulirish/5d52fb081b3570c81e3a
*/
const reflowLogger = function() {
// we don't want to redefine our properties
if (window.__reflow_log__) return;
window.__reflow_log__ = true;
const noop = () => {};
@sag1v
sag1v / withRemount.js
Created October 19, 2018 09:14
A HOC that injects a key to a react component (forcing a re-mount)
const withRemount = (Component, key) => (props) => {
const asKey = props[key];
return <Component key={asKey} {...props} />
}
@sag1v
sag1v / app count update
Created November 17, 2018 16:55
react integration with other applications article
class App extends Component {
state = {
showCounter: false,
title: 'Whaaa! cool',
currentCount: 3
}
componentDidUpdate(prevProps, prevState) {
const { showCounter, title, currentCount } = this.state;
const shouldUpdateCounter =
@sag1v
sag1v / app first mount
Created November 17, 2018 17:02
react integration with other applications article
class App extends Component {
componentDidMount(){
window.ReactCounter.mount();
}
render() {
return (
<div className="App">
<header className="App-header">
class App extends Component {
state = {
showCounter: false,
title: 'Whaaa! cool',
currentCount: 3
}
// create refs so we can pass the element to mount and unmount
counterOneRef = React.createRef();
counterTwoRef = React.createRef();
@sag1v
sag1v / app title on change
Created November 17, 2018 17:13
react integration with other applications article
class App extends Component {
state = { showCounter: false, title: 'Whaaa! cool' }
componentDidUpdate(prevProps, prevState) {
const { showCounter, title } = this.state;
const shouldUpdateCounter =
prevState.showCounter !== showCounter || prevState.title !== title;
if (shouldUpdateCounter) {
if (showCounter) {
window.ReactCounter.mount({ title });
@sag1v
sag1v / app toggle mount
Created November 17, 2018 17:15
react integration with other applications article
class App extends Component {
state = { showCounter: false }
componentDidUpdate(prevProps, prevState) {
const { showCounter } = this.state;
if (prevState.showCounter !== showCounter) {
if(showCounter){
window.ReactCounter.mount();
} else{
window.ReactCounter.unmount();
@sag1v
sag1v / app with ReactCounter
Created November 17, 2018 17:18
react integration with other applications article
class App extends Component {
state = {
showCounter: false,
title: 'Whaaa! cool',
currentCount: 3
}
toggleCounter = () => this.setState(({ showCounter }) => ({ showCounter: !showCounter }));
onTitleChange = ({ target }) => this.setState({ title: target.value });
@sag1v
sag1v / counter index global API
Created November 17, 2018 17:19
react integration with other applications article
window.ReactCounter = {
mount: () => {
const el = document.getElementById('counter-app');
ReactDOM.render(<Counter />, el);
},
unmount: () => {
const el = document.getElementById('counter-app');
ReactDOM.unmountComponentAtNode(el);
}
}