sahara-service-provider

1.1.0 • Public • Published

Example of use

import ServiceProvider from 'sahara-service-provider';

// register function
ServiceProvider.register('foo', () => {
   return 'foo'; 
});

if (ServiceProvider.has('foo')) {
    // call function
    const result = ServiceProvider.call('foo');
    // or get function
    const foo = ServiceProvider.get('foo');
    foo();
}

// unregister function
ServiceProvider.unregister('foo');

Use in ReactJS

Component App
import React, {Component} from 'react';
import ServiceProvider from 'sahara-service-provider';
import SayHelloButton from './SayHelloButton';

class App extends Component {
    constructor(props) {
        super(props);
        
        this.state = {
            text: ''
        };
        
        ServiceProvider.register('sayHello', this.sayHello.bind(this));
    }

    render() {
        return (
            <div>
                { this.state.text }
                <SayHelloButton/>
            </div>
        );
    }

    sayHello(text) {
        this.setState({text: text});
    }
}

export default App;
Component SayHelloButton
import React, {Component} from 'react';
import ServiceProvider from 'sahara-service-provider';

class SayHelloButton extends Component {
    render() {
        return (
            <button onClick={this.clickHandler}>
                Say Hello Button
            </button>
        );
    }

    clickHandler() {
        ServiceProvider.call('sayHello', 'Hello!!!');
    }
}

export default SayHelloButton;

Readme

Keywords

none

Package Sidebar

Install

npm i sahara-service-provider

Weekly Downloads

0

Version

1.1.0

License

ISC

Unpacked Size

4.02 kB

Total Files

4

Last publish

Collaborators

  • anvilapp