Higher Order Component for Managing Data
Add higher order component like martyjs' createComponent to manage data fetching and changes, etc.
Right now I am doing (ExerciseInner is the actual component):
class Exercise extends Component {
constructor(props){
super(props);
this.state = {item: realm.objects('Exercise')};
}
render() {
return <ExerciseInner {...this.props} item={this.state.item} />
}
onChange = () => {
this.setState({item: this.state.item});
};
componentDidMount() {
realm.addListener('change', this.onChange);
}
componentWillUnmount() {
realm.removeListener('change', this.onChange);
}
}
That will be a really good idea once we implement #296 and #297. Thanks for the suggestion!
For the time being I've created a NPM module called react-native-realm. It provides the ability to access/listen to realm results for the schemas that you tell it you want to listen to and then provides a mapToProps function which allows you to push realm results and your realm instance directly into your component as a prop. You can provide the full result set or run filters, sorting, etc on it before it reaches the component.