3

I have a hard time understanding and using this component:

https://www.npmjs.com/package/react-notifications-component

I can get this plugin to work easily on my App.js file as I followed the examples, but i would like to have something like a function that can be used globally in all of my components to create a notification, but I have no idea what to do.

here is something I want:

import React, {Component} from 'react';

import {createNotification} from 'somewhere..';

class RandomComponent extends Component {

      constructor(props) {
        super(props);

        this.doSomething = this.doSomething.bind(this);
      }

      doSomething() {
        /*
         * Here we do something
         */

         /* Then we create notification for something */
         createNotification();
      }

}

But then I dont know how i should Implement the createNotification function..

In my understanding I kind of need to pass the reference from App.js constructor this.notificationDOMRef = React.createRef(); to the createNotification function for this to work. Is this what I want even a correct approach or what the heck should i do. But thats kind of the way I would like to use the react-notifications-component.

Any tips on how I can do this or how I should use this to use notifications very simply in all of my components I need to use it?

Clx3
  • 119
  • 1
  • 1
  • 11
  • I'm not sure this is the correct way, but you initialize like the documentation says, and then you pass down the reference to the child classes: https://stackoverflow.com/questions/53499361/how-do-i-call-a-method-from-another-class-component-in-react-js reference = function – golddragon007 Apr 10 '19 at 14:22
  • I just don't understand how.. Like how do i make this reusable simply.. – Clx3 Apr 10 '19 at 15:35
  • it seems like react hooks could resolve easy, look it – Black Hole Apr 10 '19 at 16:14
  • If you use redux you can have a look at https://github.com/diegoddox/react-redux-toastr. Another option to pass a function as a property to show notification to the child component. – Valerii Apr 10 '19 at 16:38

0 Answers0