欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于React編寫一個全局Toast的示例代碼

 更新時間:2024年05月11日 08:28:11   作者:卸任  
前些日子在做項目的時候,需要封裝一個Toast組件,我想起之前用過的庫,只要在入口文件中引入就可以在全局中使用,還是很方便的,借這次機會也來實現(xiàn)一下,所以本文介紹了React中如何編寫一個全局Toast,需要的朋友可以參考下

前言

前些日子在做項目的時候,需要封裝一個Toast組件。我想起之前用過的庫,只要在入口文件中引入就可以在全局中使用,還是很方便的,借這次機會也來實現(xiàn)一下。說起來也算是forwardRef、useImperativeHanleuseContext的實際使用。

  • 第一種,使用forwardRefuseImperativeHanle

一個是像react-toastify庫一樣使用,在入口處放置ToastContainer,然后在代碼中任意地方使用toast("Wow so easy!")都有提示

  import React from 'react';

  import { ToastContainer, toast } from 'react-toastify';
  import 'react-toastify/dist/ReactToastify.css';
  
  function App(){
    const notify = () => toast("Wow so easy!");

    return (
      <div>
        <button onClick={notify}>Notify!</button>
        <ToastContainer />
      </div>
    );
  }
  • 第二種,使用useContext

在入口處放置ToastProvider,然后在代碼中任意地方使用 const { show } = useToast()都有提示。忘記什么庫了。

文中就用antdmessage來模擬一下我們自己寫的Toast組件。

正文

我們先來了解一下forwardRef、useImperativeHanleuseContext的基本使用。

forwardRefuseImperativeHandle 的基本使用

forwardRefuseImperativeHandle,它們通常一起使用,以便在父組件中暴露子組件的特定方法或?qū)傩浴?/p>

forwardRef,它允許你將父組件的ref轉(zhuǎn)發(fā)到子組件中的某個 DOM 節(jié)點或其他 React 組件。這樣,父組件就可以訪問子組件的引用,并直接操作它。

useImperativeHandle 是一個自定義 Hook,它允許你自定義通過 forwardRef 暴露給父組件的 ref 值。你可以指定哪些方法屬性被暴露,而不是直接暴露整個 DOM 節(jié)點或組件實例。

下面是一個簡單的例子

import React, { forwardRef, useImperativeHandle, useRef } from 'react';

const ChildComponent = forwardRef((props, ref) => {
  const inputRef = useRef(null);

  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current.focus();
    },
  }));

  return <input ref={inputRef} {...props} />;
});

const ParentComponent = () => {
  const childRef = useRef(null);

  const handleClick = () => {
    childRef.current.focus();
  };

  return (
    <div>
      <ChildComponent ref={childRef} />
      <button onClick={handleClick}>Focus Child Input</button>
    </div>
  );
};

export default ParentComponent;

使用forwardRef和useImperativeHanle封裝全局Toast

封裝組件

import React, { createRef, forwardRef, useImperativeHandle } from 'react';
import { Button, message } from 'antd';

const Toast = forwardRef((props, ref) => {
  const [messageApi, contextHolder] = message.useMessage();

  useImperativeHandle(ref, () => ({
    show: (msg: string) => {
      messageApi.info(msg);
    }
  }));

  return <>
    {contextHolder}
  </>
})

const ToastRef = createRef<{ show: (msg: string) => {} }>();

export const ToastContain = () => {
  return <Toast ref={ToastRef} />
}

export const showToast = (msg: string) => {
  if (ToastRef.current) {
    ToastRef.current.show(msg)
  }
};

在入口中引入

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import router from '@/router/index'
import reportWebVitals from './reportWebVitals';
import { RouterProvider } from 'react-router-dom';
import ErrorBoundary from './ErrorBoundary';
import { ToastContain } from './components/Toast';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
      <ToastContain />
      <RouterProvider router={router} fallbackElement={<div>準備中</div>} />
  </React.StrictMode>
);
reportWebVitals();

然后就可以在全局中使用 showToast方法了

import React from 'react';
import { showToast } from '../../../components/Toast';

export default function Index() {
  return <>
    <div
      onClick={() => {
        showToast('sadasds')
      }}
    >
       提示彈窗
    </div>
  </>
}

useContext的基本使用

useContext用于訪問組件樹中某個層級上定義的 Context。Context 提供了一種在組件之間共享值的方式,而不必通過組件樹的每個層級顯式傳遞 props。

  • 創(chuàng)建 Context

首先,你需要創(chuàng)建一個 Context 對象。這可以通過調(diào)用 React.createContext 來完成。你還可以為默認值提供一個參數(shù),如果 ContextProvider 沒有在組件樹中找到,將使用這個默認值。

import React from 'react';

const MyContext = React.createContext('defaultValue');

  • 提供 Context

你需要在組件樹中的某個地方提供這個 Context。這通常在組件的頂層完成,通過使用 MyContext.Provider組件,并傳遞一個 value prop

import React from 'react';
import MyComponent from './MyComponent';
import { MyContext } from './MyContext';

function App() {
  return (
    <MyContext.Provider value="Hello from Context">
      <MyComponent />
    </MyContext.Provider>
  );
}

export default App;

  • 使用 useContext

在需要訪問 Context 的組件中,你可以使用 useContext Hook 來獲取 Context 的當前值

import React, { useContext } from 'react';
import { MyContext } from './MyContext';

function MyComponent() {
  const contextValue = useContext(MyContext);

  return <p>Context value: {contextValue}</p>;
}

export default MyComponent;

使用useContext來封裝全局Toast

封裝組件

import React, { createContext, useCallback, useContext, useState } from 'react';
import { Button, message } from 'antd';

const ToastContext = createContext<any>(null);

export const ToastProvider = ({ children }: any) => {
  const [messageApi, contextHolder] = message.useMessage();

  const show = useCallback((msg: string) => {
    messageApi.info(msg);
  }, [messageApi]);

  return (
    <ToastContext.Provider value={{ show }}>
      {children}
      {contextHolder}
    </ToastContext.Provider>
  );
};

export const useToast = () => {
  const context = useContext(ToastContext);
  return context;
};

在入口處使用

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import router from '@/router/index'
import reportWebVitals from './reportWebVitals';
import { RouterProvider } from 'react-router-dom';
import ErrorBoundary from './ErrorBoundary';
import { ToastProvider } from './components/ToastOne';

const root = ReactDOM.createRoot(
  document.getElementById('root') as HTMLElement
);
root.render(
  <React.StrictMode>
    <ToastProvider>
      <RouterProvider router={router} fallbackElement={<div>準備中</div>} />
    </ToastProvider>
  </React.StrictMode>
);

然后就可以通過useToast在全局中使用了

import React from 'react';
import { useToast } from '../../../components/ToastOne';

export default function Index() {
  const { show } = useToast()

  return <>
    <div
      onClick={() => {
        show('guiyu')
      }}
    >
      點擊提示
    </div>
  </>
}

結(jié)尾

以上就是基于React編寫一個全局Toast的示例代碼的詳細內(nèi)容,更多關(guān)于React編寫全局Toast的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • react的hooks的用法詳解

    react的hooks的用法詳解

    這篇文章主要介紹了react的hooks的用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • 詳解React-Todos入門例子

    詳解React-Todos入門例子

    本篇文章主要介紹了React-Todos入門例子,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • React 實現(xiàn)井字棋的示例代碼

    React 實現(xiàn)井字棋的示例代碼

    本文主要介紹了React 實現(xiàn)井字棋,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • ahooks解決React閉包問題方法示例

    ahooks解決React閉包問題方法示例

    這篇文章主要為大家介紹了ahooks解決React閉包問題方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • 使用useEffect模擬組件生命周期

    使用useEffect模擬組件生命周期

    這篇文章主要介紹了使用useEffect模擬組件生命周期,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React中useTransition鉤子函數(shù)的使用詳解

    React中useTransition鉤子函數(shù)的使用詳解

    React?18的推出標志著React并發(fā)特性的正式到來,其中useTransition鉤子函數(shù)是一個重要的新增功能,下面我們就來學習一下useTransition鉤子函數(shù)的具體使用吧
    2024-02-02
  • 淺析JS中什么是自定義react數(shù)據(jù)驗證組件

    淺析JS中什么是自定義react數(shù)據(jù)驗證組件

    我們在做前端表單提交時,經(jīng)常會遇到要對表單中的數(shù)據(jù)進行校驗的問題。這篇文章主要介紹了js中什么是自定義react數(shù)據(jù)驗證組件,需要的朋友可以參考下
    2018-10-10
  • create-react-app開發(fā)常用配置教程

    create-react-app開發(fā)常用配置教程

    這篇文章主要為大家介紹了create-react-app開發(fā)常用配置教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • react顯示文件上傳進度的示例

    react顯示文件上傳進度的示例

    這篇文章主要介紹了react顯示文件上傳進度的示例,幫助大家更好的理解和學習使用react,感興趣的朋友可以了解下
    2021-04-04
  • React項目中decorators裝飾器報錯問題解決方案

    React項目中decorators裝飾器報錯問題解決方案

    這篇文章主要介紹了React項目中decorators裝飾器報錯,本文給大家分享問題所在原因及解決方案,通過圖文實例相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下
    2023-01-01

最新評論