Last active
January 21, 2018 17:45
-
-
Save jbaxleyiii/c710bb26f9cfd23e0e67d69691e676c4 to your computer and use it in GitHub Desktop.
Functional react
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 Junction from "react-junction"; | |
import { View } from "react-native"; | |
import { omit } from "ramda"; | |
import { renderComponent } from "recompose"; | |
import { graphql, gql } from "react-apollo"; | |
// the primitive styling of the component | |
const Avatar = Junction(props => ({ | |
borderRadius: 50, | |
borderSize: 2, | |
width: 100, | |
})) | |
// set primitive type | |
.as(View) | |
// hoc | |
.with(graphql(gql`{ currentPerson { photo } }`)) | |
// select props | |
.props(x => ({ | |
...x, | |
photo: x.loading ? "/default.png" : x.currentPerson.photo, | |
})) | |
// create styles based on props | |
.add(props => ({ borderColor: props.loading ? "gray" : "transparent" })) | |
// change existing style | |
.change(style => ({ ...style, borderSize: style.borderSize * 2 })) | |
// set a render function | |
// default is (props) => <Is style/class={style} >{props.children}</Is> | |
.render(props => <Image src={props.photo} />); | |
const SquareAvatar = Avatar | |
.change(omit(["borderRadius", "borderColor", "borderSize"])) | |
.change(style => ({ height: style.width })); | |
const UserCard = Junction | |
.of({ | |
width: 300, | |
height: 100, | |
}) | |
.as(View) | |
.with(graphql(gql`{ currentPerson { firstName, lastName } }`)) | |
.with(branch(props => props.loading, renderComponent(Loading))) | |
.render(props => ( | |
<View> | |
<Text>{props.firstName} {props.lastName}</Text> | |
<SquareAvatar {...props} /> | |
</View> | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment