问题
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