Last active
August 10, 2018 14:10
-
-
Save jsiwhitehead/4daa2a02195a8742a955eeb7aa8393dc to your computer and use it in GitHub Desktop.
Refluent vs standard React comparison
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 * as React from 'react'; | |
import refluent, { branch } from 'refluent'; | |
const Hover = refluent.do((_, push) => ({ | |
hoverProps: { | |
onMouseMove: () => push({ isHovered: true }), | |
onMouseLeave: () => push({ isHovered: false }), | |
}, | |
isHovered: false, | |
})); | |
export default refluent | |
.do('initial', (initial = '', push) => ({ | |
value: initial, | |
onChange: value => push({ value }), | |
})) | |
.do((props$, _) => ({ | |
submit: () => { | |
const { submit, value } = props$(); | |
if (value.length < 100) submit(); | |
}, | |
})) | |
.yield( | |
branch( | |
({ withButton }) => withButton, | |
refluent | |
.yield(Hover) | |
.do('isHovered', isHovered => ({ | |
background: isHovered ? 'red' : 'orange', | |
})) | |
.yield(({ submit, hoverProps, background, next }) => ( | |
<div> | |
{next()} | |
<p onClick={submit} {...hoverProps} style={{ background }}> | |
Submit | |
</p> | |
</div> | |
)), | |
), | |
) | |
.do((props$, _) => ({ | |
onKeyDown: e => { | |
const { submit } = props$(); | |
if (e.keyCode === 13) submit(); | |
}, | |
})) | |
.yield(({ value, onChange, onKeyDown }) => ( | |
<input | |
type="text" | |
value={value} | |
onChange={onChange} | |
onKeyDown={onKeyDown} | |
/> | |
)); |
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 * as React from 'react'; | |
class InputInner extends React.Component { | |
onKeyDown = e => { | |
if (e.keyCode === 13) this.props.submit(); | |
}; | |
render() { | |
return ( | |
<input | |
type="text" | |
value={this.props.value} | |
onChange={this.props.onChange} | |
onKeyDown={this.onKeyDown} | |
/> | |
); | |
} | |
} | |
const withHover = Comp => | |
class WithHover extends React.Component { | |
state = { | |
isHovered: false, | |
}; | |
onMouseMove = () => this.setState({ isHovered: true }); | |
onMouseLeave = () => this.setState({ isHovered: false }); | |
render() { | |
return React.createElement(Comp, { | |
hoverProps: { | |
onMouseMove: this.onMouseMove, | |
onMouseMove: this.onMouseLeave, | |
}, | |
isHovered: this.state.isHovered, | |
}); | |
} | |
}; | |
const Button = ({ value, onChange, submit, hoverProps, isHovered }) => ( | |
<div> | |
<InputInner value={value} onChange={onChange} submit={submit} /> | |
<p | |
onClick={submit} | |
{...hoverProps} | |
style={{ background: isHovered ? 'red' : 'orange' }} | |
> | |
Submit | |
</p> | |
</div> | |
); | |
const HoverButton = withHover(Button); | |
export default class ShortInput extends React.Component { | |
state = { | |
value: this.props.initial || '', | |
}; | |
onChange = value => this.setState({ value }); | |
submit = () => { | |
if (this.state.value.length < 100) this.props.submit(); | |
}; | |
render() { | |
return this.props.withButton ? ( | |
<HoverButton | |
value={this.props.value} | |
onChange={this.props.onChange} | |
submit={this.props.submit} | |
/> | |
) : ( | |
<InputInner | |
value={this.props.value} | |
onChange={this.props.onChange} | |
submit={this.props.submit} | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment