[英]React toastify called based on condition outside render
I'm trying to make a notification system in react, where I use the react-toastify
system我正在尝试在 react 中创建一个通知系统,我使用react-toastify
系统
So far I have a socket connection (with socket.io
) connected to my server, which sends a response to the client whenever a notification occurs.到目前为止,我有一个连接到我的服务器的套接字连接(使用socket.io
),只要发生通知,它就会向客户端发送响应。
const {endpoint} = this.state
const socket = socketIOClient(endpoint);
console.log("created")
socket.on('updatelogs', data =>
this.setState({logs: data},
() => createNotification(data.pop().log.level, data.pop().log.message))) // here i use a callback to create the notification
}
I have a logging system, where I take the last log
added to the stack and pass that on based on the attributes inside that.我有一个日志系统,我将最后一个添加到堆栈中的log
并根据其中的属性传递它。
Inside my createNotficiation
function I have some dummy logic like this.在我的createNotficiation
function 里面我有一些像这样的虚拟逻辑。
export const createNotification = (level, message) =>{
switch (level) {
case 'info':
return toast('info')
case 'success':
return toast("success");
case 'warning':
return toast("warning");
case 'error':
return toast("error");
}
}
Here I want to call toast
based on what is passed in, but nothing renders.在这里,我想根据传入的内容调用toast
,但没有渲染。 I have tried with a simple alert
which works fine.我尝试了一个简单的alert
,效果很好。
Here is my configuration for the toasts这是我的吐司配置
Inside App.js
: App.js
内部:
render() {
toast.configure();
return (
<div className="App">
<NewComponent
name={this.state.name} handleChange={this.setNameHandler}
preproccessHandler={this.setPreprocessHandler} isPreproccess={this.state.isProcessedData}
hasProcessedDBScan={this.state.isProcessedDBScan} setProcessedHandler={this.setProcessedHandler}
validateFileHandler={this.validateFileHandler} isUploadedFiles={this.state.isUploadedFiles}/>
<ToastContainer />
</div>
Try to render a div instead of toast with background-color as red and width as 200px.尝试渲染一个 div 而不是 toast,背景颜色为红色,宽度为 200px。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.