- import the monkey js library in your project
import monkey from 'monkey-js';
- create a new component
class Custom extends Monkey.Component {}
- use State in your component
class Custom extends Monkey.Component {
state = {
value: '',
}
render() {
const div = document.createElement('div');
div.innerHTML = this.state.value;
div.addEventListener('click', () => {
this.setState({
value: 'clicked',
});
});
return div;
}
}
- register your component outside the library
customElements.define('custom', Custom);
list of all api
-
this methodes is called when component will be added to dom
-
componentDidMount(){ console.log('componentDidMount') }
-
-
this methodes is called when component will be removed from dom
-
componentWillUnmount(){ console.log('componentWillUnmount') }
-
-
this methodes is called when component will be updated
-
componentDidUpdate(){ console.log('componentDidUpdate') }
-
-
this methodes is used to update the state of the component
-
this.setState({ text : 'new text', })
-
-
this methodes is used to render the component ui each time when state is updated the render methode is automaticaly called also to rebuild the component ui with the new value
-
render(){ return document.createElement('div'); }
-
-
this methodes is used to emit an event
-
this.emitEvent('eventName', { data: 'data', })
-
-
this property is used to store the component state to updated the state use the setState methodes ! warning : the state is not immutable , dont try to change the state directly , use the setState methodes
-
state = { text : 'text', }
-
-
this property is used to store the component props
-
this methodes is called when component will receive new props