GithubHelp home page GithubHelp logo

Using in React about javascript HOT 4 CLOSED

emitter-io avatar emitter-io commented on May 26, 2024
Using in React

from javascript.

Comments (4)

Florimond avatar Florimond commented on May 26, 2024 1

I am no React dev myself, but what I did is

npx create-react-app my-app

Then I installed emitter with nmp install emitter-io

Then I modified the app.js to obtain the following :

(sorry for the format, it's not me, it's github)

`
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
var emitter = require('emitter-io')

class App extends Component
{
constructor(props)
{
super(props)
//this.someFunction = this.someFunction.bind(this)
let client = emitter.connect();
client.subscribe({
key: "YgBLv3zD4e2Vz_jdZUR1ojZlJiABlhTD",
channel: "test"
});

	client.on('message', function(msg){
		console.log( msg.asString() );
	});

	// publish a message to the chat channel
	client.publish({
		key: "YgBLv3zD4e2Vz_jdZUR1ojZlJiABlhTD",
		channel: "test",
		message: "hello, emitter!"
	});
}    

render() {
return (
...
`

And this works fine. But maybe I'm not using React the proper way? Tell me more about how you do it...

from javascript.

Florimond avatar Florimond commented on May 26, 2024 1

Not sure I understand the issue... You can register any function in client.on('message'), including an arrow function of the component. From this arrow function, can't you just do this.setState({...}) ?

from javascript.

fauzanmasulili avatar fauzanmasulili commented on May 26, 2024

how to pass message to state?

from javascript.

fauzanmasulili avatar fauzanmasulili commented on May 26, 2024

thanks man, it works using an arrow function

from javascript.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.