在ReactJS中的渲染内部调用一个动作-不能在调度中间进行调度 [英] Calling an action inside render in ReactJS - cannot dispatch in the middle of dispatch

查看:84
本文介绍了在ReactJS中的渲染内部调用一个动作-不能在调度中间进行调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理在Instagram上搜索公共和私人帐户的问题。为了提供简短的背景信息,我尝试查询Instagram上的所有用户,但由于我无权访问 target_user_is_private (我们提交的应用程序没有 follower_list 作用域),我只是要检查该州的数据ID,并以这种方式进行处理。

I'm dealing with searching for public and private accounts on Instagram. To give a brief context, I'm trying to query all the users on Instagram but since I don't have access to target_user_is_private (our submitted app doesn't have the follower_list scope) I'm just going to check for the state's data id and approach it that way.

使用以下代码遇到无法在调度中间进行调度的问题-解决此问题的方法是什么?

I'm getting an issue of "Cannot dispatch in the middle of dispatch" with the code below - what is the solution to this problem?

render: () ->
    if !@state.data.id
        return (
            # An action here that I want to call that notifies the user "Cannot track private accounts"
        )

    else
        return (
            <div>
                <h1>Returned!</h1>
            </div>
        )

我在网上进行调查,并得知我不应该直接打电话给我,而是要通过商店电话吗?

I research online and was informed that I shouldn't call an action directly, but go through the store call?

推荐答案

render方法在组件的生命周期中将被多次调用。 React将在后台运行此方法以在虚拟Dom中进行比较。因此,您永远都不应在render内部触发外部方法。

The render method will be called multiple times during the component's lifecycle. React will run this method behind the scenes to compare in the virtual Dom. You shouldn't ever have external methods fired inside of render as a result.

您实际上何时希望触发该方法?何时首次安装组件?在这种情况下,正确的钩子应该是 ComponentDidMount

When do you actually want the method to fire? When the component is first mounted? In that case, the correct hook would be ComponentDidMount.

这篇关于在ReactJS中的渲染内部调用一个动作-不能在调度中间进行调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆