realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Higher Order Component for Managing Data

Open cancan101 opened this issue 10 years ago • 2 comments

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);
  }
}

cancan101 avatar Mar 30 '16 17:03 cancan101

That will be a really good idea once we implement #296 and #297. Thanks for the suggestion!

appden avatar Mar 30 '16 20:03 appden

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.

jasonmerino avatar Jun 07 '17 14:06 jasonmerino