Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we have an example of how to use notistack in functional component? #205

Closed
nilamsavani opened this issue Dec 16, 2019 · 4 comments
Closed

Comments

@nilamsavani
Copy link

Hi,

I want to do the exact thing as done in https://codesandbox.io/s/github/iamhosseindhv/notistack/tree/master/examples/redux-example but I am using functional component.

Can anyone here create an example which has same functionality using the functional component?

@iamhosseindhv
Copy link
Owner

iamhosseindhv commented Dec 16, 2019

Let me know if this is buggy @nilamsavani, so I can add it to the official redux example as well.

https://codesandbox.io/s/notistack-redux-example-nj0nu

@wilsonj806
Copy link

The example looks solid, but I noticed that it doesn't use the useDispatch and useSelector hooks provided by the latest React Redux release(v7.1.x).

If that's not intentional, I can fork and post an updated CodeSandbox because I'm already trying use Notistack with the new React Redux hooks.

@iamhosseindhv
Copy link
Owner

Thanks @wilsonj806. redux-example now uses hooks only.

@wilsonj806
Copy link

wilsonj806 commented Feb 1, 2020

Looks good @iamhosseindhv! Just a small suggestion, but I think it's better to have the FunctionalNotifier store the displayed notifications like the below:

const FunctionalNotifier = () => {
  // React Redux hooks
  const dispatch = useDispatch();
  const notifications = useSelector(state => state.app.notifications);

  // Store the displayed notifications in component state
  const [displayed, setDisplayed] = useState([]);

  const storeDisplayed = id => {
    setDisplayed(prevDisplayed => [...prevDisplayed, id]);
  };

  const removeDisplayed = id => {
    setDisplayed(prevDisplayed => [...prevDisplayed.filter(key => id !== key)]);
  };

  // doMoreStuffBelow()
}

That way it's clearer that Notifier tracks and updates displayed notifications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants