-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a HoC as an alternative to the mixin
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import React from 'react'; | ||
import {ensure} from './util'; | ||
import {update, unmount} from './Mixin'; | ||
|
||
// Mixin for RethinkDB query subscription support in React components. You'll | ||
// generally want to use DefaultHoC or PropsHoC, which use BaseHoC to | ||
// create more usable versions. | ||
// | ||
// In your component, you should define an observe(props, state) function that | ||
// returns an object mapping query names to QueryRequests. See | ||
// QueryRequest.js for the API. | ||
// | ||
// In the child component, you will have access to this.props.data, which is an | ||
// object mapping from the same query names returned in observe() to the | ||
// results of each query as an QueryResult. See QueryResult.js for the | ||
// API. | ||
// | ||
// Here is a simple example of the mixin API: | ||
// const observe = (props, state) => ({ | ||
// turtles: new QueryRequest({ | ||
// query: r.table('turtles'), | ||
// changes: true, | ||
// initial: [], | ||
// }), | ||
// }); | ||
|
||
// class App extends Component { | ||
// render() { | ||
// return <div> | ||
// {this.props.data.turtles.value().map(function(x) { | ||
// return <div key={x.id}>{x.firstName}</div>; | ||
// })}; | ||
// </div>; | ||
// }, | ||
// }; | ||
|
||
// BaseHoC(new Session())(observe)(App); | ||
|
||
export const BaseHoC = sessionGetter => observe => ChildComponent => class ReactRethinkDB extends React.Component { | ||
constructor(props, state) { | ||
super(); | ||
this.observe = observe; | ||
} | ||
|
||
componentWillMount() { | ||
const session = sessionGetter(this); | ||
this.dispatch = session.runQuery.bind(session); | ||
ensure(session && session._subscriptionManager, | ||
`Must define Session`); | ||
ensure(this.observe, `Must define observe()`); | ||
ensure(session._connPromise, `Must connect() before mounting react-rethinkdb`); | ||
this._rethinkMixinState = {session, subscriptions: {}}; | ||
this.data = this.data || {}; | ||
update(this, this.props, this.state); | ||
} | ||
|
||
componentDidMount() { | ||
this._rethinkMixinState.isMounted = true; | ||
} | ||
|
||
componentWillUnmount() { | ||
unmount(this); | ||
this._rethinkMixinState.isMounted = false; | ||
} | ||
|
||
componentWillUpdate(nextProps, nextState) { | ||
if (nextProps !== this.props || nextState !== this.state) { | ||
update(this, nextProps, nextState); | ||
} | ||
} | ||
|
||
render() { | ||
return <ChildComponent data={this.data} dispatch={this.dispatch} {...this.props} />; | ||
} | ||
}; | ||
|
||
// HoC that uses rethink session from props. For example: | ||
// class MyComponent extends Component { | ||
// ... | ||
// }); | ||
// var session = new Session(); | ||
// React.render(<MyComponent rethinkSession={session} />, mountNode); | ||
export const PropsHoC = name => BaseHoC(component => component.props[name]); |
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
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