- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为calendarLogicHandler()
的函数,它设置state
,将整个月分散到其中。我在 componentDidMount()
中调用了它。
state
集适用于我正在处理的日历app
。该日历工具将成为 Yaga 类(class)应用
的一部分。
然后在该工具的周 View 模式(显示一周的所有事件)上,我调用 weekAgendaLogicHandler()
,它会过滤当前月份的当前周并将其显示在应用程序
。按照我的逻辑,本周和当月的 state
会被进一步操纵,但在这里显示它是没有意义的。
问题是:
我无法在 ComponentDidMount()
上访问 this.state.currrentMonth
并且不知何故,当我尝试 console.log(this.state.currentMonth )
在 render()
上我看到它渲染 this.state.currentMonth
两次空白,然后,在第三次重新渲染时它最终得到 this.state.currentMonth
由 calendarLogicHandler()
设置。
current month in render() []
current month in render() []
getting current month in componentDidMount []
current month in render() [Array[7], Array[7], Array[7], Array[7], Array[7]]
current month in render() [Array[7], Array[7], Array[7], Array[7], Array[7]]
[]
如何正确使用此函数,以便我可以调用 weekAgendaLogicHandler()
而无需在 的
,并在渲染阶段之前设置我的状态,换句话说,在 callback
function
中调用它this.setState({currentMonth})componentdidmount
之前?
代码是here及以下:
import React from "react";
import "./styles.css";
export default class App extends React.Component {
state = {
currentWeek: [],
currentMonth: [],
monthGetter: new Date().getMonth(),
yearGetter: new Date().getFullYear()
};
componentDidMount() {
this.calendarLogicHandler();
this.weekAgendaLogicHandler();
console.log(
"getting current month in componentDidMount",
this.state.currentMonth
);
}
weekAgendaLogicHandler = () => {
let currentWeek = this.state.currentMonth
.filter(week => week.includes(new Date().setHours(0, 0, 0, 0)))
.flat();
this.setState({ currentWeek }, () => console.log(this.state.currentWeek));
};
calendarLogicHandler = () => {
const oneDay = 86400000;
const lastDayOfTheMonth = new Date(
this.state.yearGetter,
this.state.monthGetter + 1,
0
).getDate();
let currentMonth = [
new Date(this.state.yearGetter, this.state.monthGetter, 1).valueOf()
]; //starts on month day 1
for (let i = 1; i < lastDayOfTheMonth; i++) {
//push the entire month
currentMonth.push(Math.max.apply(null, currentMonth) + oneDay);
}
//localize the first date of the month dates array and check what day of the week it is
//spread the days of the week, which are the remaining days of prev month to fill calendar first week
for (let i = new Date(Math.min(...currentMonth)).getDay(); i > 0; i--) {
currentMonth.unshift(Math.min.apply(null, currentMonth) - oneDay);
}
//spread the days of the week, which are the remaining days of prev month to fill calendar last week
for (let i = new Date(Math.max(...currentMonth)).getDay(); i < 6; i++) {
currentMonth.push(Math.max.apply(null, currentMonth) + oneDay);
}
let monthInWeeks = [];
for (let i = 0; i < currentMonth.length; i += 7) {
let chunk = currentMonth.slice(i, i + 7);
monthInWeeks.push(chunk);
}
currentMonth = monthInWeeks;
this.setState({ currentMonth });
};
render() {
console.log("current month in render()", this.state.currentMonth);
return (
<div className="App">
<h1>Testing componentDidMount</h1>
</div>
);
}
}
最佳答案
这就是 React 的工作方式。
生命周期render()
每次更新时都会调用方法。而且它也在 componentDidMount
之前被调用。 。根据生命周期名称componentDidMount
表示组件安装后调用的函数。由于组件已经 mounted
,所以render()
componentDidMount
之前已经调用过方法方法。
所以,当您第一次获得空白数据时,即在 componentDidMount
之前, render()
正在以初始状态调用,即 []
再次,可能有一些东西正在更新,可能在weekAgendaLogicHandler()
中功能再次render()
被调用时显示空白 []
数组,即初始状态。
现在,第三次,this.setState 正在发生,状态正在更新,然后 render()
您正在获得所需的数组。
所以,这就是 render()
生命周期方法有效。
现在,如果您想使用render
仅包含数据,而不包含 null
数据。然后,您可以显示某种加载程序。
即,
render(){
if(this.state.currentMonth.length){
return(
<div className="App">
<h1>Testing componentDidMount</h1>
</div>
)
}
else{
return(
<p>Loading data...</p>
)
}
}
关于javascript - 如何通过 ComponentDidMount() 中调用的函数访问状态集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60976818/
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
PowerShell Web Access 允许您通过 Web 浏览器运行 PowerShell cmdlet。它显示了一个基于 Web 的控制台窗口。 有没有办法运行 cmdlet 而无需在控制台窗
我尝试在无需用户登录的情况下访问 Sharepoint 文件。 我可以通过以下任一方式获取访问 token 方法一: var client = new RestClient("https://logi
我目前正在尝试通过 Chrome 扩展程序访问 Google 服务。我的理解是,对于 JS 应用程序,Google 首选的身份验证机制是 OAuth。我的应用目前已成功通过 OAuth 向服务进行身份
假设我有纯抽象类 IHandler 和派生自它的类: class IHandler { public: virtual int process_input(char input) = 0; };
我有一个带有 ThymeLeaf 和 Dojo 的 Spring 应用程序,这给我带来了问题。当我从我的 HTML 文件中引用 CSS 文件时,它们在 Firebug 中显示为中止。但是,当我通过在地
这个问题已经有答案了: JavaScript property access: dot notation vs. brackets? (17 个回答) 已关闭 6 年前。 为什么这不起作用? func
我想将所有流量重定向到 https,只有 robot.txt 应该可以通过 http 访问。 是否可以为 robot.txt 文件创建异常(exception)? 我的 .htaccess 文件: R
我遇到了 LinkedIn OAuth2: "Unable to verify access token" 中描述的相同问题;但是,那里描述的解决方案并不能解决我的问题。 我能够成功请求访问 toke
问题 我有一个暴露给 *:8080 的 Docker 服务容器. 我无法通过 localhost:8080 访问容器. Chrome /curl无限期挂断。 但是如果我使用任何其他本地IP,我就可以访
我正在使用 Google 的 Oauth 2.0 来获取用户的 access_token,但我不知道如何将它与 imaplib 一起使用来访问收件箱。 最佳答案 下面是带有 oauth 2.0 的 I
我正在做 docker 入门指南:https://docs.docker.com/get-started/part3/#recap-and-cheat-sheet-optional docker-co
我正在尝试使用静态 IP 在 AKS 上创建一个 Web 应用程序,自然找到了一个带有 Nginx ingress controller in Azure's documentation 的解决方案。
这是我在名为 foo.js 的文件中的代码。 console.log('module.exports:', module.exports) console.log('module.id:', modu
我试图理解访问键。我读过https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-se
我正在使用 MGTwitterEngine"将 twitter 集成到我的应用程序中。它在 iOS 4.2 上运行良好。当我尝试从任何 iOS 5 设备访问 twitter 时,我遇到了身份验证 to
我试图理解访问键。我读过https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-se
我正在使用以下 API 列出我的 Facebook 好友。 https://graph.facebook.com/me/friends?access_token= ??? 我想知道访问 token 过
401 Unauthorized - Show headers - { "error": { "errors": [ { "domain": "global", "reas
我已经将我的 django 应用程序部署到 heroku 并使用 Amazon s3 存储桶存储静态文件,我发现从 s3 存储桶到 heroku 获取数据没有问题。但是,当我测试查看内容存储位置时,除
我是一名优秀的程序员,十分优秀!