Meteor + React + Mantra: Use LocalState to pass action result to a component?

Hi guys, I’m using the LocalState to send back actions result to a component and I’m wondering if it’s the good way to do that.

Action:

Meteor.call('flats.search', params, (err, flats) => {

  if (err) {
    LocalState.set('SEARCH_FLAT_ERROR', err.message);
    LocalState.set('FLAT_RESULT', NO_FLAT_FOUND);
    return null;
  }

  LocalState.set('FLAT_RESULT', flats);
});

Container:

const flats = LocalState.get('FLAT_RESULT') ? LocalState.get('FLAT_RESULT') : []; onData(null, {errorMessages, flats});

Component:

const {flats} = this.props;
{flats.map( (flat, idx) => (
   ....
 )}

Is there a better way to do that, if yes please help me !!

Thanks

This topic is bit old but actual in my case. Is this way of getting data is recommended?
@ngouana did you managed to find any other solutions if not is the above solution works for you?

Hi @mipasov, Using the LocalState is actually the recommended pattern as pointed in Mantra specifications.
https://kadirahq.github.io/mantra/#sec-State-Management

1 Like

Hi @ngouana ,

I have little difficulties in realizing this in my code.
In my container I am subscribing to some data and passing data trough onData() . Could you please give some hints, how to pass data which set through actions again by onData(), on same container?

Something like:

container: 
subscribe to publication.
  if no error
     onData(null,{data})

actions:
   call method
      if no error
         LocalState.set('DATA',data);


on same container: 
  const data=LocalState.get('DATA');  
\\how it will be possible to pass data ? 

Regards,
Mirlan

Hi @mipasov,

this link should help: