Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm new to reactjs. I want to know Is there an equivalent for angular services such as $rootScop, $q, $webSocket, ... in reactJs?

.service('a', function ($rootScope, $location, $q, $webSocket) {
    this.init = function () {
        b()
        c()
    }

For example code parameters above what equivalent in react? I know the equivalent $scope in react is this.state.

share|improve this question

There is no such thing as services in react

here are alternatives.

  1. $rootscope --> state, you can share it across components. (You can use redux for state management whose philosophy is one true source of data).
  2. $q --> Es6 Promise
  3. $websocket --> html5 websocket.

Some thing similar to service is you can write a Class or Function which takes all the required services as params and you can call it any where by exporting it.

some similar implementation you can use for react.

In service.js

const myService = (...otherServices) => {
    doStuff1();
    doStuff2();
    return {
       ...items
    }
}
export default myService;

In component.js

you can import it

import myService from './service';
import React from 'react';
class testComponent extends React.Component {
    constructor(props){
        super(props);
        this.data = myService().getData(); //just an example.
    }
    render(){
        return (
            <div>{this.data}</div>
        );
    }
} 
share|improve this answer
    
$http => axios (github.com/mzabriskie/axios). There are others but this one will be familiar coming from $http. – John F. Oct 24 at 13:54
    
These are just one alternative i can think of on top of my mind. there are many alternatives we can use and new ones keep coming. – Neel Oct 25 at 8:55

$rootScope -> it is an global scope object in angular, in react we use reducers to store data which will be accessible to all components

$q-> we have q library same as $q in react

$location -> we have transition/history inside instance of class/components

$webScocket-> t here are multiple modules https://blog.pusher.com/making-reactjs-realtime-with-websockets/

share|improve this answer
    
q library: Es6 Promise? OR react-q ? – Digipng Oct 24 at 12:51
    
you can use react-q which is npm module which allow you to create your own promise/ allow synchronus programming – Akash Bhandwalkar Oct 24 at 13:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.