Silind-Software / direflow

🧩 Use the best of two worlds. Create fast, performant, native Web Components using React.
https://direflow.io
MIT License
502 stars 77 forks source link

React class component with custom event #272

Closed hirenreshamwala closed 2 years ago

hirenreshamwala commented 2 years ago

Hello,

Any example of React class component with the custom event?

hirenreshamwala commented 2 years ago

I found

import {Component} from "react";
import {EventContext} from "direflow-component";
import PropTypes from 'prop-types';

class App extends Component {

    constructor(props, context) {
        super(props);
        this.dispatchEvent = context;
    }

    fireExampleEvent() {
        let {example} = this.state,
            event = new CustomEvent('example', {'detail': 'example data'});
        this.dispatchEvent(event);
    }
    render() {
        return (
            <>
                <button onClick={this.fireExampleEvent.bind(this)}>Click</button>
            </>
        )
    }
}

App.contextType = EventContext;
App.defaultProps = {
    name: ''
}
App.propTypes = {
    name: PropTypes.string
};

export default App;