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 { | |
state = { clickCount: 0 } | |
canClick() { | |
return this.state.clickCount < 4; | |
} | |
onIncrement() { | |
this.setState(state => ({ | |
clickCount: state.clickCount + 1, |
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
<Toggle> | |
{({ on, onToggle }) => ( | |
<Switch on={on} onToggle={onToggle} /> | |
)} | |
</Toggle> |
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
export default class Toggle extends Component { | |
state = { on: false } | |
onClick = (e) => { | |
e.preventDefault(); | |
this.setState(state => ({ | |
on: !state.on, | |
})); | |
} |
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
const Switch = ({ on, onToggle }) => ( | |
<label className={styles.switch} onClick={onToggle}> | |
<input | |
checked={on} | |
type="checkbox" | |
/> | |
<span className={styles.slider} /> | |
</label> | |
); |
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
const mousePosition = () => (BaseComponent) => { | |
return class MousePosition extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { x: 0, y: 0 }; | |
} | |
componentDidMount() { | |
this.listener = (e) => { | |
this.setState(() => ({ x: e.pageX, y: e.pageY })); |
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
const tween = ({ tween, duration = 1000, defaultStart, end }) => (BaseComponent) => { | |
return class Tween extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
timeStart: +new Date(), | |
start: defaultStart(props) || 0, | |
current: defaultStart(props) || 0, | |
}; |
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 { compose, mapProps } from 'recompose'; | |
const startX = 0; | |
const startY = 0; | |
const FollowingRectangle = compose( | |
mousePosition(), | |
tween({ | |
tween: linear, | |
duration: 1000, |
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
const mousePosition = () => (BaseComponent) => { | |
return class MousePosition extends Component { | |
// ... | |
render() { | |
const { x, y } = this.state; | |
return <BaseComponent {...this.props} x={x} y={y} />; | |
} | |
} |
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
const tween = ({ tween, duration = 1000, defaultStart, end }) => (BaseComponent) => { | |
return class Tween extends Component { | |
// ... | |
render() { | |
const { current } = this.state; | |
return <BaseComponent {...this.props} current={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 createMatrix(a = 1, b = a) { | |
return Array.from({ length: a }, () => Array.from({ length: b }, () => 0)); | |
} | |
// 1 x 10 Matrix | |
console.log(createMatrix(1, 10)); | |
// Square Matrix | |
console.log(createMatrix(3)); |