How to use PubSub-JS in Different Files

0

I'm doing an application with React, I want to save data with the pubsub-js library, the scheme is as follows.

I have two components that are in different files: cliente.js and conta.js

In the file cliente.js I have a method, which opens a channel with PubSub.publish and saves an array with objects, then goes to file conta.js

  import PubSub from 'pubsub-js';

  pegaValor() {
    console.log(PubSub.publish('cliente-p', [{nome:'rodrigo'}]));
    window.location.href = "http://localhost:3000/conta";
  }

render() {
    return (
       <button type="button" onClick={this.pegaValor} className="btn btn-secondary">Conta</button>
);
}

In the file conta.js , I'm trying to get this data with PubSub.subscribe .

 import PubSub from 'pubsub-js';

export default class Conta extends Component {

  constructor() {
    super();
  }

  componentWillMount() {

    PubSub.subscribe('cliente-p', function(topico, valor){
      console.log('Valores: ', valor);
    })
  }
}

But it does not show any value in console in file conta.js , What should I do to get the data that is coming from another file?

    
asked by anonymous 21.08.2018 / 20:47

0 answers