ElJefeJames
ElJefeJames

Reputation: 295

How do I update Meteor Subscription Parameters within React

I'm trying to implement a Meteor Publish/Subscribe method that searches the database for a parameter. How do I update this subscription parameter with new search terms? Do I need to close down previous subscriptions? Are there any common patterns for this?

Currently on client (ES6) NOT WORKING

    constructor() {
        super();
        const searchSubscription =  Meteor.subscribe("search", this.state.searchQuery)

        this.state = {
          searchQuery: "fringe",
          searchSubscription: searchSubscription
      }
}

I can update the state but can't figure out how to pass the state variable "searchQuery" into the subscription.

Upvotes: 1

Views: 779

Answers (1)

MatiK
MatiK

Reputation: 573

See here: https://guide.meteor.com/react.html#data

You can use react-meteor-data official meteor package to reactively response to data changes

Use createContainer method from this package and use it like this:

import { createContainer } from 'meteor/react-meteor-data';

export default FooContainer = createContainer(() => {
  // Do all your reactive data access in this method.
  // Note that this subscription will get cleaned up when your component is unmounted
  var handle = Meteor.subscribe("todoList", this.props.id);

  return {
    currentUser: Meteor.user(),
    listLoading: ! handle.ready(),
    tasks: Tasks.find({listId: this.props.id}).fetch(),
  };
}, Foo);

Upvotes: 1

Related Questions