admin管理员组文章数量:1208153
Note: I don't really have an example code to present since I do not have any code related component injection (aside from these functional components that I have which are very self explanatory). I'll keep it short to save you time.
Let's say I have a <Toast />
component. I want to be able to "inject" that component to any page components. This will save me many lines of codes because I do not have to write conditions on every page component.
Note: I don't really have an example code to present since I do not have any code related component injection (aside from these functional components that I have which are very self explanatory). I'll keep it short to save you time.
Let's say I have a <Toast />
component. I want to be able to "inject" that component to any page components. This will save me many lines of codes because I do not have to write conditions on every page component.
- 2 Import the component and return it in your render function? Please show an example. – 0stone0 Commented Jan 20 at 9:48
1 Answer
Reset to default -1There are two ways of doing so. Depends what you need.
- Composition with children (docs)
Toast component receives the page content as children and renders it alongside the toast message based on the showToast prop. Simple and easy to understand. Good for cases where toast logic is directly tied to the parent. But it's less flexible if you need more complex toast management like multiple toasts, queued toasts, or toasts triggered from deep within the child component's logic.
Example:
import React, { useState } from 'react';
const Toast = ({ children, showToast, toastMessage }) => {
return (
<div>
{showToast && <div>{toastMessage}</div>}
{children}
</div>
);
};
const PageComponent = () => {
const [showToast, setShowToast] = useState(true);
const [toastMessage, setToastMessage] = useState("Example toast");
return (
<Toast showToast={showToast} toastMessage={toastMessage}>
<div>
<h1>My Page</h1>
<p>Some content here...</p>
<button onClick={() => setShowToast(false)}>Hide Toast</button>
</div>
</Toast>
);
};
export default PageComponent;
- Context API (docs)
ToastProvider component holds the state and logic for all toasts, effectively making toast management global. Any component can use the useToast hook to interact with the toast system, adding or removing toast messages. This is straightforward for simple use cases. Good for when you want to trigger toasts from anywhere in your application. But it becomes more complex if you need finer-grained control over individual toasts, such as styling or positioning specific toasts based on where they originate.
Example:
import React, { createContext, useContext, useState } from 'react';
const ToastContext = createContext();
const ToastProvider = ({ children }) => {
const [toasts, setToasts] = useState([]);
const addToast = (message) => {
setToasts([...toasts, { id: Date.now(), message }]);
};
const removeToast = (id) => {
setToasts(toasts.filter((toast) => toast.id !== id));
};
return (
<ToastContext.Provider value={{ toasts, addToast, removeToast }}>
{children}
{toasts.map((toast) => (
<div key={toast.id}>
{toast.message}
<button onClick={() => removeToast(toast.id)}>Close</button>
</div>
))}
</ToastContext.Provider>
);
};
const useToast = () => {
return useContext(ToastContext);
};
const PageComponent = () => {
const { addToast } = useToast();
return (
<div>
<h1>My Page</h1>
<button onClick={() => addToast("Toast message from PageComponent")}>Show Toast</button>
</div>
);
};
const App = () => {
return (
<ToastProvider>
<PageComponent />
</ToastProvider>
);
};
export default App;
本文标签: reactjsHow to inject a component to another componentStack Overflow
版权声明:本文标题:reactjs - How to inject a component to another component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738708292a2108045.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论