0

I am building a React-redux library that displays a widget chat. The library should provide an interface allowing the client to configure and understand the state of the library and, render widgets into their document.

I use Webpack for the build process. I use Twilio-Chat library for the chat features.

My index file looks like this:

Index.js

import MyChatWidget from 'components/MyChatWidget';
export default {
widgets: {
   MyChatWidget: {
      render: (args) => {
          ReactDOM.render(
              <MyChatWidget />
          );
      },
      logout: () => {
         // this function should call a the logout in the MyChatWidget 
         // React Component
      }
   }
}

MyChatWidget

import Chat from 'twilio-chat';
class MyChatWidget extends Component {
    logout() {
        Chat.shutdown()
    }
}

export default connect()(MyChatWidget);

The logout is exposed to the client and it should call a function inside the MyChatWidget component.

How can I achieve this behaviour? Am I including the Twilio Chat in the wrong place(MyChatWidget Component)?

I read this article for the building with webpack (https://codeburst.io/building-react-widget-libraries-using-webpack-e0a140c16ce4)

2 Answers 2

1

Disclaimer: I'm not a huge fan of putting anything on the window object, especially because if you use server rendering the window isn't available. But passing an instance of your class to the window object would make it accessible anywhere JS has access to the window object.

constructor() { super(); if ( window ) { window.mychatwidget = this; } }

Sign up to request clarification or add additional context in comments.

Comments

0

You can declare the logout method static and access that method as MyChatWidget.logout() in your index.js

2 Comments

Also there is no reason to have logout(): () => rather than logout: () =>
logout(): () => was a syntax error. Sorry I cannot have the logout method static because I need to use this inside

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.