Skip to content

Instantly share code, notes, and snippets.

@EmperorEarth
Last active January 29, 2017 17:43
Show Gist options
  • Select an option

  • Save EmperorEarth/36394d45ff3deebed99be8fb4f7a6938 to your computer and use it in GitHub Desktop.

Select an option

Save EmperorEarth/36394d45ff3deebed99be8fb4f7a6938 to your computer and use it in GitHub Desktop.
RFC, Relay derivation
Container-local reasoning
Make containers able to fetch their own data without having to notify every ancestor
Drag-n-droppable components/containers
Root entry (usually index.js) takes in the 'schema.json' or 'schema.graphql' as a dependency, and parses schema
Every fragment and root query will be available then to every container
Containers can then fetch their own data without having to ask their parents and their parents... to do it
From the example below, you can introduce/remove <UserProfilePic /> to/from <Foo /> or <Bar /> without having to tell 20+ parents.
class UserProfilePic extends React.Component {
render() {return (
<img src={this.props.imgURL} />
)}
constructor(props) {super(props)}
}
export default Relay.createContainer(UserProfilePic, {
childProps: {
imgURL: () => Relay.QL`fragment on User(${this.props.userID}) {imgURL}`
},
// also pass through all props
// so child would get userID
}
// some random component somewhere
class Foo extends React.Component {
render() {return (
<UserProfilePic userID="1" />
)}
}
// some other random component somewhere
class Bar extends React.Component {
render() {return (
<UserProfilePic userID="2" />
)}
}
import React from 'react'
import ReactDOM from 'react-dom'
import Relay from 'react-relay'
import Schema from './schema.json'
import App from './App.js'
// Relay parses the schema in root entry
const env = Relay.Store.Parse(Schema)
ReactDOM.render(
<Relay.Root
env={env}
container={App}
/>,
document.getElementById('mount')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment