reactjs - 如何在 SnackbarProvider 的内容中获取变量值
问题描述
我正在使用notistack库snackbar
在我的应用程序中显示。我想自定义小吃吧的内容。因此使用content
的属性snackbar
。我想知道消息变体是success
or warning
or error
,并且基于此我想设置颜色。吐司。下面是一个沙盒 URL:
https ://codesandbox.io/s/optimistic-hellman-2gcs0?file=/App.js
我怎样才能得到里面的变量值ToastTemplate
?
解决方案
在你的export const enqueueSnackbar = (notification) => {
函数里面你有一个notification
对象,它有你可以使用的options
和variant
:
export const enqueueSnackbar = (notification) => {
const key = notification.options && notification.options.key;
const variant = notification.options && notification.options.variant;
console.log(variant);
console.log(notification);
return {
type: ENQUEUE_SNACKBAR,
notification: {
...notification,
key: key || new Date().getTime() + Math.random(),
},
};
};
更新
由于您想在内部进行变体SnackbarProvider
,因此您可以传递整个数据(而不仅仅是文本消息)并在内部使用您需要的内容。
export const enqueueSnackbar = notification => {
const key = notification.options && notification.options.key;
return {
type: ENQUEUE_SNACKBAR,
notification: {
message: { ...notification },
// ^ Instead of passing the text - I'm passing the entire object
key: key || new Date().getTime() + Math.random()
}
};
};
在 SnackbarProvider 内部:
<SnackbarProvider
content={(key, notificationData) => {
const message = notificationData.message;
const variant = notificationData.options.variant
console.log(variant);
return <ToastTemplate id={key} message={message} />;
}}
>
在此处查看工作示例:https ://codesandbox.io/s/flamboyant-ramanujan-1p7yw?file=/index.js
推荐阅读
- kubernetes - Kubectl 补丁添加额外的“-”字符
- karate - 在 js 函数中使用调用 read
- scala - 需要关于返回值的解释:“用于递归调用的scala中的理解”
- html - HTML的选择/选项上不需要的边距底部
- python - 在不使用用户函数的情况下将图像保存在具有不同名称的 For 循环中 (sckit - Python)
- jquery - 动态创建的元素不改变 CSS 没有第一次点击
- hibernate - Corda 4.1:MappedSchemas 之间的交叉引用
- extjs - beforerender 侦听器中的 SetValue 不更新任何内容
- api - docker 映像无法正常工作或运行
- java - JMockit MockUp 多种方法