sahara-service-provider
v1.1.0
Published
```js import ServiceProvider from 'sahara-service-provider';
Downloads
2
Readme
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;