'Socket.io + React: Different classes -> use same socket?
This is hopefully my best take at describing and simplifying my problem:
I have my main class for my react app called App in App.js (I use [...] as Placeholder for other code):
import {myComponent} from "./components.js";
const { io } = require("socket.io-client");
class App extends Component {
constructor(props) {
super(props)
this.state = {
isLoaded
[...]
}
}
componentDidMount() {
this.socket = io()
[...]
}
render() {
const {isLoaded, [...]} = this.state
return (
<h1>BLUB</h1>
<myComponent/>
[...]
)
}
}
myComponent is defined in components.js and imported into App.js. I have also implemeted socket.io following the instructions in their documentation. As you see on componentDidMount() I define this.socket for the communication to the server. My question is how can I tell myComponent to use this specific socket as well, eventough it is located in components.js?
class myComponent extends Component {
???
}
export {myComponent}
Thanks for every piece of advice :)
PS: I moved myComponent out of App.js and the class App, to reduce the class size, it was getting out of control...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|