23mofang-react-native-root-toast 中文文档教程

发布于 7年前 浏览 13 项目主页 更新于 3年前

react-native-root-toast


Features

  1. Pure javascript solution.
  2. Support both Android and iOS.
  3. Lots of custom options for Toast.
  4. You can show/hide Toast by calling api or using Component inside render.

screen-shoots

Install

npm install react-native-root-toast

Settings

NameDefaultTypeDescription
durationToast.durations.SHORTNumberThe duration of the toast. (Only for api calling method)
visiblefalseBoolThe visibility of toast. (Only for Toast Component)
positionToast.positions.BOTTOMNumberThe position of toast showing on screen (A negative number represents the distance from the bottom of screen. A positive number represents the distance form the top of screen. 0 will position the toast to the middle of screen.)
animationtrueBoolShould preform an animation on toast appearing or disappearing.
shadowtrueBoolShould drop shadow around Toast element.
backgroundColornullStringThe background color of the toast.
shadowColornullStringThe shadow color of the toast.
textColornullStringThe text color of the toast.
delay0NumberThe delay duration before toast start appearing on screen.
hideOnPresstrueBoolShould hide toast that appears by pressing on the toast.
onShownullFunctionCallback for toast`s appear animation start
onShownnullFunctionCallback for toast`s appear animation end
onHidenullFunctionCallback for toast`s hide animation start
onHiddennullFunctionCallback for toast`s hide animation end

Properties

Toast.durations

预设 toast 持续时间。

  1. Toast.durations.SHORT(等于2000

  2. Toast.durations.LONG(等于3500

Toast.positions

预置吐司的位置。

  1. Toast.positions.TOP(等于20

  2. Toast.positions.BOTTOM(等于-20

  3. Toast.positions.CENTER(等于0 >)

Usage

有两种不同的方法来管理 Toast。

Calling api
import Toast from 'react-native-root-toast';


// Add a Toast on screen.
let toast = Toast.show('This is a message', {
    duration: Toast.durations.LONG,
    position: Toast.positions.BOTTOM,
    shadow: true,
    animation: true,
    hideOnPress: true,
    delay: 0,
    onShow: () => {
        // calls on toast\`s appear animation start
    },
    onShown: () => {
        // calls on toast\`s appear animation end.
    },
    onHide: () => {
        // calls on toast\`s hide animation start.
    },
    onHidden: () => {
        // calls on toast\`s hide animation end.
    }
});

// You can manually hide the Toast, or it will automatically disappear after a `duration` ms timeout.
setTimeout(function () {
    Toast.hide(toast);
}, 500);
Using a Component

注意: 使用 Component inside render 显示 toast,当 卸载时,toast 会自动消失。

import React, {Component} from 'react-native';
import Toast from 'react-native-root-toast';

class Example extends Component{
    constructor() {
        super(...arguments);
        this.state = {
            visible: false
        };
    }

    componentDidMount() {
        setTimeout(() => this.setState({
            visible: true
        }), 2000); // show toast after 2s

        setTimeout(() => this.setState({
            visible: false
        }), 5000); // hide toast after 5s
    };

    render() {
        return <Toast
            visible={this.state.visible}
            position={50}
            shadow={false}
            animation={false}
            hideOnPress={true}
        >This is a message</Toast>;
    }
}

Run example:

cd ./Example
npm install

react-native-root-toast


Features

  1. Pure javascript solution.
  2. Support both Android and iOS.
  3. Lots of custom options for Toast.
  4. You can show/hide Toast by calling api or using Component inside render.

screen-shoots

Install

npm install react-native-root-toast

Settings

NameDefaultTypeDescription
durationToast.durations.SHORTNumberThe duration of the toast. (Only for api calling method)
visiblefalseBoolThe visibility of toast. (Only for Toast Component)
positionToast.positions.BOTTOMNumberThe position of toast showing on screen (A negative number represents the distance from the bottom of screen. A positive number represents the distance form the top of screen. 0 will position the toast to the middle of screen.)
animationtrueBoolShould preform an animation on toast appearing or disappearing.
shadowtrueBoolShould drop shadow around Toast element.
backgroundColornullStringThe background color of the toast.
shadowColornullStringThe shadow color of the toast.
textColornullStringThe text color of the toast.
delay0NumberThe delay duration before toast start appearing on screen.
hideOnPresstrueBoolShould hide toast that appears by pressing on the toast.
onShownullFunctionCallback for toast`s appear animation start
onShownnullFunctionCallback for toast`s appear animation end
onHidenullFunctionCallback for toast`s hide animation start
onHiddennullFunctionCallback for toast`s hide animation end

Properties

Toast.durations

presets of duration of the toast.

  1. Toast.durations.SHORT (equals to 2000)

  2. Toast.durations.LONG (equals to 3500)

Toast.positions

presets of position of toast.

  1. Toast.positions.TOP (equals to 20)

  2. Toast.positions.BOTTOM (equals to -20)

  3. Toast.positions.CENTER (equals to 0)

Usage

There are two different ways to manage a Toast.

Calling api
import Toast from 'react-native-root-toast';


// Add a Toast on screen.
let toast = Toast.show('This is a message', {
    duration: Toast.durations.LONG,
    position: Toast.positions.BOTTOM,
    shadow: true,
    animation: true,
    hideOnPress: true,
    delay: 0,
    onShow: () => {
        // calls on toast\`s appear animation start
    },
    onShown: () => {
        // calls on toast\`s appear animation end.
    },
    onHide: () => {
        // calls on toast\`s hide animation start.
    },
    onHidden: () => {
        // calls on toast\`s hide animation end.
    }
});

// You can manually hide the Toast, or it will automatically disappear after a `duration` ms timeout.
setTimeout(function () {
    Toast.hide(toast);
}, 500);
Using a Component

NOTE: Showing a toast by using a Component inside render, The toast will be automatically disappeared when the <Toast /> is unmounted.

import React, {Component} from 'react-native';
import Toast from 'react-native-root-toast';

class Example extends Component{
    constructor() {
        super(...arguments);
        this.state = {
            visible: false
        };
    }

    componentDidMount() {
        setTimeout(() => this.setState({
            visible: true
        }), 2000); // show toast after 2s

        setTimeout(() => this.setState({
            visible: false
        }), 5000); // hide toast after 5s
    };

    render() {
        return <Toast
            visible={this.state.visible}
            position={50}
            shadow={false}
            animation={false}
            hideOnPress={true}
        >This is a message</Toast>;
    }
}

Run example:

cd ./Example
npm install
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文