如何重新渲染组件而不是调用构造函数?

时间:2019-05-07 17:12:51

标签: javascript react-native

我有一个FlatList,其中的项目带有进度条,我想更改一个项目的进度。对于进度条,我制作了一个自定义组件。但是,每当我更改一项的进度时,他都会重新渲染该项目,但是对象不会重新渲染,而是会调用构造函数。由于进度动画,这是有问题的。

我尝试添加诸如componentWillReceiveProps和componentWillUpdate之类的函数,但是这些函数不会被调用。

项目:

x

组件:

import numpy as np
look_back = 5 ## This is the number of points to include in each iteration
x = np.arange(0,100)
new_array = []
for num in range(0, len(x)-look_back):
   new_array.append(x[num:num+look_back])
new_array = np.array(new_array)

print (np.array(x).shape) ## old array = (100,)
print (new_array.shape) ## new array = (95,5)

我不想让构造函数进行校准并重置组件,而是希望它使用新的道具重新渲染。

1 个答案:

答案 0 :(得分:0)

通过插入如下所示的条件语句来更改要运行的代码。

example.js

componentDidUpdate(){
   if(true){
   ...
  }
}

OR

您可以在每次更改代码时再次呈现它。

example.js

render{
//your code
return();
}

程序运行顺序 enter image description here