react-toast-notifications icon indicating copy to clipboard operation
react-toast-notifications copied to clipboard

Support multiple nested Contexts

Open jesstelford opened this issue 5 years ago • 2 comments

NOTE: PR best viewed with whitespace changes hidden

From the updated docs:

Displaying individual toasts differently is done using nested <ToastProvider>s.

For example, the docs page displays both "notification" style & "snack bar" style toasts simultaneously on the same page.

Nested Providers must be given a unique name prop, which is then also passed to the <ToastConsumer> component or useToasts() hook.

import { ToastProvider, ToastConsumer } from 'react-toast-notifications';
const App = () => (
  <ToastProvider>
    <ToastProvider name="snack">
      <ToastConsumer>
        {({ add }) => <button onClick={() => add('A toast')}>Add Toast</button>}
      </ToastConsumer>
      <ToastConsumer name="snack">
        {({ add }) => <button onClick={() => add('A snack')}>Add Snack</button>}
      </ToastConsumer>
    </ToastProvider>
  </ToastProvider>
);

jesstelford avatar Jun 12 '20 03:06 jesstelford

I've published the changes in this PR as @jesstelford/[email protected] to try it out:

yarn add @jesstelford/[email protected]

jesstelford avatar Jun 12 '20 05:06 jesstelford

I have tested the PR by @jesstelford! It works like a charm thank you. Please merge!

soosap avatar Nov 10 '20 18:11 soosap