场景 - 数据库中某些表的数据更改,例如使用来自另一个进程的文件上传,例如不属于 react-redux 应用程序的 ETL 工具。那么我们如何刷新 react-redux 应用程序组件呢? Web 套接字是唯一将这种更改从服务器推送到客户端的方法吗?或者我们是否定期刷新受影响的组件?但是所有这些端口都必须一直保持打开状态吗?后端是一个 java/oracle 应用程序。 谢谢
最佳答案
您应该在每次加载相关 React 组件时对数据库进行 Ajax 调用(我喜欢使用 axios),以确保数据是最新的。如果您使用 React.createClass
创建组件,请在所述组件内使用 getInitialState()
。
编辑:由于您不希望组件在每次加载时都查询数据库,而是仅在指定的时间段之后查询,因此我建议对以下代码进行修改:
getInitialState() {
//Define a variable and set as the current date object
let date = new Date;
//Convert the variable's value to a string
date = date.toString();
//Slice the day of the month from the date string
let day = date.slice(6,8);
//Query the database if it's the 15th or 28th (approximately every 2
//weeks, accounting for February
if (day == ("15" || "28")) {
//Ajax call here
return {data: ajaxResponse};
}
}
请注意,每次在所选日期加载组件时,此代码都会查询数据库。由于日期对象精确到毫秒,您可以将时间窗口设置得尽可能窄。请记住,如果在此期间从未加载组件,则不会查询数据库,直到再次满足 if
条件。
关于reactjs - 当应用程序外部的数据库上的数据更改时如何刷新 React Redux 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48835601/