Guest User

Untitled

a guest
Nov 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3.  
  4. class ElmHandler extends React.Component {
  5. node = React.createRef()
  6.  
  7. // Setup the Elm app using the ref for this components node passing in flags passed in props. Connect the port functions from JS that have been passed in props to the ports established in the initialized Elm app for use during component update.
  8. componentDidMount () {
  9. const { src, flags, ports } = this.props
  10. this.app = src.init({
  11. node: this.node.current,
  12. flags
  13. })
  14.  
  15. if (ports) {
  16. this.ports = ports(this.app.ports)
  17. }
  18. }
  19.  
  20. // Passes new props through ports so we can capture updates that want from JS -> Elm, but always return false because Elm handles all DOM managemant for this node and its children and we do not want React to re-render omce Elm is initialized.
  21. shouldComponentUpdate (nextProps) {
  22. if (this.ports) {
  23. this.ports(nextProps)
  24. }
  25. return false
  26. }
  27.  
  28. render () {
  29. return (
  30. <div ref={this.node} />
  31. )
  32. }
  33. }
  34.  
  35. ElmHandler.propTypes = {
  36. flags: PropTypes.object,
  37. ports: PropTypes.func,
  38. src: PropTypes.object.isRequired
  39. }
  40.  
  41. export default ElmHandler
Add Comment
Please, Sign In to add comment