Last active
August 30, 2017 15:49
-
-
Save kof/2cb595d3b9401d9d76e718fe8dc53ca4 to your computer and use it in GitHub Desktop.
Reactive Component
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 MyComponent extends Component { | |
componentWillMount() { | |
alert('will mount') | |
} | |
onClick = () => { | |
alert('Button Clicked') | |
// Forward click to the outer listeners. | |
this.props.onClick() | |
} | |
render() { | |
return ( | |
<div> | |
<Button | |
disabled={this.props.disabled} | |
onClick={this.onClick} | |
> | |
{this.props.text} | |
</Button> | |
</div> | |
) | |
} | |
} |
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
// Using https://github.com/tc39/proposal-observable | |
const render = ({lifecycle, disabled, text}) => { | |
let myButton | |
const container = div({ | |
children: [myButton = button({disabled, children: text, lifecycle})] | |
}) | |
return {container, myButton} | |
} | |
const MyComponent = (props) => { | |
const {container, myButton} = render(props) | |
const events = new Observable((observer) => { | |
myButton.events.subscribe({next: (event) => { | |
alert('Button event', event) | |
// Forward click to outer listeners. | |
observer.next(event) | |
}}) | |
}) | |
props.lifecycle.subscribe({next: (state) => { | |
alert('lifecycle', state) // for e.g. willMount | |
}}) | |
return {renderable: container.renderable, events} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Todo: