渲染内部触发setState的函数

时间:2019-12-17 22:31:16

标签: reactjs

在我的import pandas as pd from bokeh.palettes import RdBu from bokeh.models import LinearColorMapper, ColumnDataSource, ColorBar from bokeh.models.ranges import FactorRange from bokeh.plotting import figure, show from bokeh.io import output_notebook import numpy as np output_notebook() d = dict(x = ['A','A','A', 'B','B','B','C','C','C','D','D','D'], y = ['B','C','D', 'A','C','D','B','D','A','A','B','C'], corr = np.random.uniform(low=-1, high=1, size=(12,)).tolist()) df = pd.DataFrame(d) df['size'] = np.where(df['corr']<0, np.abs(df['corr']), df['corr'])*50 #added a new column to make the plot size colors = list(reversed(RdBu[9])) exp_cmap = LinearColorMapper(palette=colors, low = -1, high = 1) p = figure(x_range = FactorRange(), y_range = FactorRange(), plot_width=700, plot_height=450, title="Correlation", toolbar_location=None, tools="hover") p.scatter("x","y",source=df, fill_alpha=1, line_width=0, size="size", fill_color={"field":"corr", "transform":exp_cmap}) p.x_range.factors = sorted(df['x'].unique().tolist()) p.y_range.factors = sorted(df['y'].unique().tolist(), reverse = True) p.xaxis.axis_label = 'Values' p.yaxis.axis_label = 'Values' bar = ColorBar(color_mapper=exp_cmap, location=(0,0)) p.add_layout(bar, "right") show(p) 中,我触发了一个将用户重定向到其他页面的函数,但出现错误render

我注意到,如果我将Cannot update during an existing state transition (such as within render)中的redirectToPath函数放在render中,它将允许状态完成转换并且错误消失了。如果将setTimeouthistory.push(path)(在setState内)放在redirectToPath中,它也可以工作。我该如何重构它,以免与状态转换发生冲突?

setTimeout

1 个答案:

答案 0 :(得分:1)

那是处理您的负载的怪异方法。也许您可以向我们解释您背后的逻辑?

同时,您可以尝试一下

代替:

<div>Done Loading</div>

使用:

<Redirect to="/somewhere/else" />

有关工作示例,请参阅文档:https://reacttraining.com/react-router/web/api/Redirect