DEV Community

Altencir Junior
Altencir Junior

Posted on

1

Criando mensagens Toast em React Native

Muitas vezes desejamos mostrar uma mensagem de alerta na tela de um usuário e podemos usar diversos meios como um Text ou até um Alert para aviso. Entretanto o React tem a sua disponibilidade os Toast de notificação. Neste artigo iremos criar Toasts. Ele fornece dois tipos de toasts: sucesso e erro, que são personalizáveis ​​por meio do objeto ToastConfig fornecido.

Instalação da biblioteca Toast -

Para usar este componente, instale-o como um pacote npm:

npm install --save react-native-toast-message
yarn add react-native-toast-message
Enter fullscreen mode Exit fullscreen mode

Utilizando o Toast-

Importe o Provider componente do pacote e envolva-o em seu componente principal:

import Toast from "react-native-toast-message";

const App = () => {
  return (
    <View>
      {/* Your main component */}
      <Toast.Provider />
    </View>
  );
};
Enter fullscreen mode Exit fullscreen mode

Isso cria um provedor toast que pode ser usado para mostrar mensagens toast. Para mostrar uma mensagem toast, basta chamar o método showSuccess ou showError do Toast objeto:

import Toast from "react-native-toast-message";

const ExampleComponent = () => {
  const handleButtonClick = () => {
    Toast.showSuccess("Success message");
    Toast.showError("Error message");
  };

  return (
    <View>
      <Button title="Show Toast" onPress={handleButtonClick} />
    </View>
  );
};
Enter fullscreen mode Exit fullscreen mode

Por padrão, o componente será exibido na parte inferior da tela, acima do teclado (se estiver visível). Você pode personalizar a aparência do brinde passando um ToastConfigobjeto para o componente Provider:

import Toast from "react-native-toast-message";

const config = {
  successStyle: {
    backgroundColor: "#00FF00",
  },
  errorStyle: {
    backgroundColor: "#FF0000",
  },
  textStyle: {
    color: "#FFFFFF",
  },
};

const App = () => {
  return (
    <View>
      {/* Your main component */}
      <Toast.Provider config={config} />
    </View>
  );
};
Enter fullscreen mode Exit fullscreen mode

Com isso, conseguimos ver o quão fácil é utilizar o Toast. Espero que este artigo tenha o ajudado a criar mensagens de notificação para sua aplicação mobile feita em React Native. Obrigado por ler até aqui.

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay