Calling an action inside render in ReactJS - cannot dispatch in the middle of dispatch

邮差的信 提交于 2019-12-25 06:31:13

问题


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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/38728212/calling-an-action-inside-render-in-reactjs-cannot-dispatch-in-the-middle-of-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!