react-native-easy-toast icon indicating copy to clipboard operation
react-native-easy-toast copied to clipboard

Is it possible to use this toast messages in action(Redux) files like an alert

Open ramviki opened this issue 6 years ago • 8 comments

ramviki avatar Apr 20 '18 06:04 ramviki

I also want to trigger toasts from elsewhere, not in render of components. Could this be possible?

Edit: I found another package as wrapper around ToastAndroid working also for iOS: https://github.com/onemolegames/react-native-toast-native

donni106 avatar May 08 '18 16:05 donni106

Same goes for me. Although I prefer to use this library since it is pure JavaScript.

Any ideas?

miguelocarvajal avatar May 20 '18 20:05 miguelocarvajal

can create a connected component in app root that shows toaster when some value gets updated.

nol13 avatar Oct 04 '18 03:10 nol13

@nol13 do you mind show an example?

samw2k00 avatar Oct 11 '18 04:10 samw2k00

class RootToaster extends PureComponent {
 componentDidUpdate(prevProps) {
    if (this.props.alert && this.props.alert !== prevProps.alert) { 
        this.refs.toast.show(this.props.alert.message); 
    }
 }

 render() {
    return <Toast ref="toast"/>;
 }
}

export const ConnectedRootToaster = connect(({alerts}) => ({ alert: alerts.alert}))(RootToaster)

So will show toast every time alerts reducer updates alert object.

nol13 avatar Oct 11 '18 15:10 nol13

class RootToaster extends PureComponent {
 componentDidUpdate(prevProps) {
    if (this.props.alert && this.props.alert !== prevProps.alert) { 
        this.refs.toast.show(this.props.alert.message); 
    }
 }

 render() {
    return <Toast ref="toast"/>;
 }
}

export const ConnectedRootToaster = connect(({alerts}) => ({ alert: alerts.alert}))(RootToaster)

So will show toast every time alerts reducer updates alert object.

Thank you very much!, it worked like a charm :)

mrcarjul avatar May 09 '19 22:05 mrcarjul

class RootToaster extends PureComponent {
 componentDidUpdate(prevProps) {
    if (this.props.alert && this.props.alert !== prevProps.alert) { 
        this.refs.toast.show(this.props.alert.message); 
    }
 }

 render() {
    return <Toast ref="toast"/>;
 }
}

export const ConnectedRootToaster = connect(({alerts}) => ({ alert: alerts.alert}))(RootToaster)

So will show toast every time alerts reducer updates alert object.

how use this with hooks?

this doesn't work....

 useEffect(() => {
        displayAlert();
    }, props.alert);

    const displayAlert=()=> {
        if (props.alert !== null && this_toast.current != null) {
            this_toast.current.show(props.alert.message);
        }
    }

thank you

fredattack avatar Jan 30 '20 05:01 fredattack

class RootToaster extends PureComponent {
 componentDidUpdate(prevProps) {
    if (this.props.alert && this.props.alert !== prevProps.alert) { 
        this.refs.toast.show(this.props.alert.message); 
    }
 }

 render() {
    return <Toast ref="toast"/>;
 }
}

export const ConnectedRootToaster = connect(({alerts}) => ({ alert: alerts.alert}))(RootToaster)

So will show toast every time alerts reducer updates alert object.

how use this with hooks?

this doesn't work....

 useEffect(() => {
        displayAlert();
    }, props.alert);

    const displayAlert=()=> {
        if (props.alert !== null && this_toast.current != null) {
            this_toast.current.show(props.alert.message);
        }
    }

thank you

import React, { useEffect, useRef } from 'react';
import { connect } from 'react-redux';
import Toast from 'react-native-easy-toast';
import { Colors } from '../../Config';

const RootToaster = (props) => {

  useEffect(() => {
    if (props.toastDetail) {
      toast.current.show(props.toastDetail, 2000);
    }
  }, [props]);

  const toast = useRef(null);

  return (
    <Toast
      ref={toast}
      style={{ backgroundColor: Colors.supernova, borderRadius: 25, padding: 10 }}
      position="bottom"
      positionValue={75}
      fadeInDuration={750}
      fadeOutDuration={1000}
    />
  );
};

export default connect((state) => ({ toastDetail: state.events.toastDetails }))(RootToaster);

You can use it as & it's working fine.

hth-developers avatar Aug 09 '21 12:08 hth-developers