小编muk*_*inx的帖子

为什么getstrivedstatefromprops在setstate之后被调用?

React引入了getDerivedStateFromProps(props, state)在每个渲染方法之前调用的新静态方法,但为什么呢?在改变道具之后调用它对我来说是有意义的,但在setState它没有之后,也许我错过了一些东西.

datePicker根据公司要求创建了一个组件,组件日期由prop控制.我在组件中有以下状态.

selectedDate: number;
selectedMonth: number;
selectedYear: number;
currentMonth: number;
currentYear: number;
view: string;
Run Code Online (Sandbox Code Playgroud)

selected表示从日期道具派生的选定日期,currentMonthcurrentYear表示当前日历视图中的月份和年份.

如果date从道具的变化selected*,currentMonthcurrentYear应相应改变.为此,我正在使用,getDerivedStateFromProps但是让用户点击月份名称将日历视图切换到月份(而不是显示月份的日期名称),该函数currentMonth使用setState 更新为此,但是prop的日期与之前(包含前一个月)应该,但是getDerivedStateFromProps被调用,currentMonth再次与之前相同,而不是更改.

我正在创建一个额外的变量state来跟踪是否getDerivedStateFromProps被调用,setState但我不认为这是正确的方法.

无论是我做错了什么或丢失了什么或者getDerivedStateFromProps不应该被打电话setState.可能我做错了什么.

reactjs getderivedstatefromprops

23
推荐指数
3
解决办法
4623
查看次数

这个循环将如何结束

再次打开编辑:如何结束这一个

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<string> s;
    string word;

    while(cin >> word)
        {
            s.push_back(word);
        }



        for(auto i =0; i < s.size(); i++)
        {
            for(auto &c : s[i])
            c = toupper(c);
        }

        int j=1;
        for(auto c : s)
        {
              cout << c << " ";
              if(j%8==0)
              {
                  cout << "\n";
              }
              j++;
        }  
}
Run Code Online (Sandbox Code Playgroud)
  1. 可以使用其他方法,如在循环中放置单词!="end"或类似的东西,但它将创建和我不想要的额外单词.

  2. 我没有得到一个东西,为什么当我在两个单词之间给出空格,比如,你好我的名字是詹姆斯(在输入中)然后为什么c ++在不同的向量块中将它视为不同的字符串和strors.我是c ++编程的新手,你可以看到,但是一个老C程序员,不是很老的3个月大的大学生.


这是书籍c ++入门第5版的一个例子.我的问题是这个while循环将如何结束我尝试了所有的东西,比如输入,输入0这里有很多例子就像这样.

int main()
{
      vector<unsigned> scores(11, 0);          
      unsigned grade;
      while (cin >> grade)
      { …
Run Code Online (Sandbox Code Playgroud)

c++ while-loop

4
推荐指数
1
解决办法
140
查看次数