Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Make branch return a stateless functional component #273

Merged
merged 1 commit into from
Nov 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions src/packages/recompose/branch.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
import React from 'react'
import createHelper from './createHelper'
import createEagerFactory from './createEagerFactory'

const identity = component => component
const identity = Component => Component

const branch = (test, left, right = identity) => BaseComponent =>
class extends React.Component {
constructor(props, context) {
super(props, context)
this.computeChildComponent(this.props)
}

computeChildComponent(props) {
if (test(props)) {
this.leftFactory =
this.leftFactory || createEagerFactory(left(BaseComponent))
this.factory = this.leftFactory
} else {
this.rightFactory =
this.rightFactory || createEagerFactory(right(BaseComponent))
this.factory = this.rightFactory
}
}

componentWillReceiveProps(nextProps) {
this.computeChildComponent(nextProps)
}

render() {
return this.factory(this.props)
const branch = (test, left, right = identity) => BaseComponent => {
let leftFactory
let rightFactory
return props => {
if (test(props)) {
leftFactory = leftFactory || createEagerFactory(left(BaseComponent))
return leftFactory(props)
}
rightFactory = rightFactory || createEagerFactory(right(BaseComponent))
return rightFactory(props)
}
}

export default createHelper(branch, 'branch')