calintamas / react-native-toast-message

Animated toast message component for React Native
MIT License
1.67k stars 258 forks source link

How can we use multiple Toast at the same time #104

Open khelifioussama opened 3 years ago

khelifioussama commented 3 years ago

if I have 2 Toasts , I receive only the Last Toast

francisleigh commented 3 years ago

Yes, this feature would be great!

johannbuscail commented 3 years ago

Has this been implemented yet ?

nightness commented 3 years ago

Huge reason, I'm about to write my own toast. You can't show more than one at a time, or at least one after another. There is no queue for rapidly added toast messages. ;(

nightness commented 3 years ago

The data needs to be statefully put in to an array. This works for me... https://snack.expo.io/@nightness/toast-snack---bottom

erhanersoz commented 3 years ago

I queued the toasts. Maybe it works for you too. https://snack.expo.io/@erhanersoz/toastqueue

omeranar1 commented 2 years ago

did you solve the problem Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

@erhanersoz


import React, { useState, useEffect, useRef } from 'react';
import { View, TouchableOpacity, Text, StyleSheet } from 'react-native';
import Toast from 'react-native-toast-message';

export default function App() {
   const toastRef = useRef();
  //const toastRef = React.forwardRef((props, ref) => <Toast ref={ref} {...props} />);
  const [toastQueue, setToastQueue] = useState([]);
  const [toastId, setToastId] = useState(1);
  const [toastVisible, setToastVisible] = useState(false);
  const [shownToast, setShownToast] = useState(null);
  let toastTimeout;

  const addToast = () => {
    setToastQueue([...toastQueue, {
      id: toastId,
      text1: toastId.toString(),
      autoHide: false,
      onPress: () => {
        toastRef.current.hide();
      },
      onHide: () => {
        clearTimeout(toastTimeout);
        setShownToast(toastId);
        setToastVisible(false);
      },
      onShow: () => {
        setToastVisible(true);
        toastTimeout = setTimeout(() => {
          toastRef.current.hide();
        }, 4000);
      }
    }]);
    setToastId(toastId + 1);
  };

  useEffect(() => {
    if (toastQueue.length > 0 && !toastVisible) {
      if(toastRef.current!=null)
      { 
      toastRef.current.show(toastQueue[0]);
      setToastVisible(true);
      }
    } 
  }, [toastQueue, toastVisible]);

  useEffect(() => {
    if (toastQueue.length > 0 && shownToast) {
      setToastQueue([...toastQueue.filter((item) => item.id !== shownToast)]);
      setShownToast(null);
    }
  }, [toastQueue, shownToast]);

  return (
    <>
      <View style={styles.container}>
        <TouchableOpacity style={styles.button} onPress={() => addToast()}>
          <Text>Create New Toast</Text>
        </TouchableOpacity>
      </View>
      <Toast ref={toastRef} />
    </>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  button: {
    backgroundColor: '#a5e9a0',
    paddingVertical: 15,
    paddingHorizontal: 30,
  }
});
erhanersoz commented 2 years ago

@omeranar1 I don't remember. 😞

omeranar1 commented 2 years ago

thank you, I found another package similar to this repo with supporting multiple notifications. problem solved. react-native-toast-notifications

ArturoTorresMartinez commented 1 year ago

Any news about this?

enestatli commented 1 year ago

Any news about this?

We exactly needed same feature in one of our applications so developed a toaster component which supports queue. You can find module here https://www.npmjs.com/package/react-native-toastable

ammarfaris commented 10 months ago

this will be an essential feature.. thanks @calintamas

FabianMontoya commented 1 month ago

Any update?

iway1 commented 1 month ago

FYI it's not necessary to put this into state at all can use a global variable to prevent rerenders.