import React, {useState, useEffect} from 'react';
import NavbarComponent from './NavbarComponent';
import { Card, Button, Modal } from "react-bootstrap";
import axios from 'axios';
function Notifications({username}) {
const [show, setShow] = useState(false);
const [response, setresponse] = useState([])
const handleClose = () => setShow(false);
const handleShow = () => setShow(true);
const [notifications, setnotifictions] = useState([])
const [password, setpassword] = useState("")
var user = {
username: username
}
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
useEffect(() => {
const fetchnoti = async () => {
await axios.post('/api/getnoti', user, {cancelToken: source.token})
.then(res => {
console.log(res)
setnotifictions(res.data)
})
.catch(err => {
if (axios.isCancel(err)) {
console.log('Request canceled', err.message);
}
else console.log(err)
})
}
fetchnoti();
return () => {
source.cancel()
}
}, [])
function accept(noti){
var param = {
noti: noti,
password: password
}
axios.post('/api/accepttxn', param)
.then(res => {
console.log(res)
setresponse(res.data)
})
.catch(err => {
console.log(err)
})
}
function accepttxn(noti){
handleShow();
setresponse([]);
setpassword("");
return(
<div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Enter Password to Proceed</Modal.Title>
</Modal.Header>
<Modal.Body><input type="password" value={password} onChange={(event) => setpassword(event.target.value)}/></Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Close
</Button>
<Button variant="success" onClick={() => {accept(noti)}}>
Proceed to Accept
</Button>
{response}
</Modal.Footer>
</Modal>
</div>
);
}
function reject(noti){
var param = {
noti: noti,
password: password
}
axios.post('/api/rejecttxn', param)
.then(res => {
console.log(res)
setresponse(res.data)
})
.catch(err => {
console.log(err)
})
}
function rejecttxn(noti){
handleShow();
setresponse([]);
setpassword("");
return(
<div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Enter Password to Proceed</Modal.Title>
</Modal.Header>
<Modal.Body><input type="password" value={password} onChange={(event) => setpassword(event.target.value)}/></Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Close
</Button>
<Button variant="danger" onClick={() => {reject(noti)}}>
Proceed to Reject
</Button>
{response}
</Modal.Footer>
</Modal>
</div>
);
}
const notis = notifications.map((noti) => {
return(
<Card key={noti.id}>
<Card.Body>
<Card.Text>
Sender: {noti.sender}
</Card.Text>
<Card.Text>
Amount: {noti.amount}
</Card.Text>
<Card.Text>
{noti.message ? <div>Message: {noti.message}</div> : null}
</Card.Text>
<Button variant="primary"
onClick={
accepttxn(noti)
}>Accept</Button>
<Button variant="danger"
onClick={
rejecttxn(noti)
}>Reject</Button>
</Card.Body>
</Card>
)
})
return (
<div>
{notis.length ? <div>{notis}</div> : <h1>No new notifications</h1>}
</div>
);
}
export default Notifications;
有人能帮帮我吗?
这可能不是useEffect,你有一个按钮,它在函数呈现时调用函数:
<Button variant="danger" onClick={rejecttxn(noti)}>Reject</Button>
您可以重写函数以返回函数,这可以提高性能,因为在使用内联函数时,您可以重新呈现不需要重新呈现的组件,请检查下面的示例:
function rejecttxn(noti){
return function() {
handleShow();
setresponse([]);
setpassword("");
return (
<div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Enter Password to Proceed</Modal.Title>
</Modal.Header>
<Modal.Body><input type="password" value={password} onChange={(event) => setpassword(event.target.value)}/></Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Close
</Button>
<Button variant="danger" onClick={() => {reject(noti)}}>
Proceed to Reject
</Button>
{response}
</Modal.Footer>
</Modal>
</div>
);
}
}
或使用箭头函数:
const rejecttxn = (noti) => () => {
handleShow();
setresponse([]);
setpassword("");
return (
<div>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Enter Password to Proceed</Modal.Title>
</Modal.Header>
<Modal.Body><input type="password" value={password} onChange={(event) => setpassword(event.target.value)}/></Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={handleClose}>
Close
</Button>
<Button variant="danger" onClick={() => {reject(noti)}}>
Proceed to Reject
</Button>
{response}
</Modal.Footer>
</Modal>
</div>
);
}
}
呈现以下组件时出现错误: 太多的重放。React限制呈现的数量以防止无限循环。?
我在使用时遇到一个错误。即使加载页面是新鲜的,没有按下任何按钮,我的按钮onClick事件侦听器激活了,正如我之前在主题中提到的,我的错误: “错误:重新呈现太多。React限制呈现次数以防止无限循环。”
当GetCreatorLicense()被禁用时,programe运行良好,我试图在循环之外设置数据,但仍然得到‘错误:太多的重新呈现’。React限制呈现的次数,以防止无限循环。错误,然后我把这个函数放在useEffect下,我只得到控制台输出,但useState没有改变
常量plusCount=()=>{ 常量minsCount=()=>{ 第二:不要在UI上给我发送错误:太多的重新呈现。React限制呈现次数以防止无限循环。 导出默认应用程序; 猜测:为什么这条消息两次都显示我错误,但第一次让我在UI上显示,第二次不显示我在UI上
斯塔克布利茨链接。
我很困惑为什么Iam会出现这个错误:太多的重新呈现。React限制呈现次数以防止无限循环。当我尝试调用USESTAT保存checkBoxList数据时,就会发生这种情况。如果我注释掉行setListOptions(checkBoxList);错误消失了。我希望发生的是,setListoptions应该保存在本地,然后我可以调用listOptions来映射我的数据。谢谢你的协助。