简体   繁体   English

当用户添加项目时,如何发送敬酒消息?

[英]How to give a toast message, when user add an item?

import React from 'react';

import { useForm } from "react-hook-form";从“react-hook-form”导入 { useForm }; import { toast } from 'react-toastify';从'react-toastify'导入{ toast};

const AddStorage = () => { const { register, handleSubmit } = useForm(); const AddStorage = () => { const { register, handleSubmit } = useForm();

const onSubmit = data => {
    console.log(data);
    const url = `https://desolate-springs-82685.herokuapp.com/storage`;
    fetch(url, {
        method: 'POST',
        headers: {
            'content-type': 'application/json'
        },
        body: JSON.stringify(data)
    })
        .then(res => res.json())
        .then(result => {
            console.log(result);
             toast('Congrats! You added an order');
        })
};

return (
    <div className='w-50 mx-auto mb-5 mt-5 pb-5 pt-5'>
        <h2 className='text-center mb-4' >Please add a Storage</h2>
        <form className='d-flex flex-column' onSubmit={handleSubmit(onSubmit)}>
            <input className='mb-2' placeholder='Photo URL' type="text" {...register("img")} />
            <input className='mb-2' placeholder='Name' {...register("name", { required: true, maxLength: 20 })} />
            <input className='mb-2' placeholder='Capacity' type="number and text" {...register("capacity")} />
            <textarea className='mb-3' placeholder='Description' {...register("description")} />
            <input className='btn-success mb-4' type="submit" value="Add Storage" />
        </form>
    </div>
);

}; };

export default AddStorage;导出默认的 AddStorage;

您需要将ToastContainer添加到您的组件中,然后只需像这样使用toast函数:

toast.success('YOUR MESSAGE',{TOAST CONFIG})

First you need to import ToastContainer from the package library like this首先,您需要像这样从包库中导入 ToastContainer

import { ToastContainer, toast } from 'react-toastify';

Read more about usage of ToastContainer component in the link链接中阅读有关 ToastContainer 组件用法的更多信息

ToastContainer is a container , where you want to show your error message , how to use it ToastContainer 是一个容器,你想在其中显示你的错误信息,如何使用它

import * as React from 'react';
import { ToastContainer, toast } from 'react-toastify';

const Articles = () => {
    const markAsHome = () => {
        toast.success(`Article successfuly defined as a recent article` );
    }
    return (
        <>
            <ToastContainer />
            <button onClick={markAsHome}> Mark As Home </button>
        </>
    );
}

export default Articles;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM