Using onTypingEnd to update state results in
kirkryan opened this issue · 7 comments
Hi,
I want to create an interactive onboarding screen using your awesome typewriter effect (brings back nostalgia for old school RPG games!). I would like to display a paragraph of text, then prompt for input (i.e. what is your name?). Then another paragraph of text and prompt, etc.
I thought that a good way of doing this would be to use the onTypingEnd function, however, it results in a "Maximum update depth exceeded error - This can happen when a component repeatedly calls setState inside component will update or componentDidUpdate. React limits the number of nested updates to prevent infinite loops".
<TypeWriter typing={1} onTypingEnd={this.setState({ hasFinishedTyping: true })} > Hi there! My name is Cyclo! What is your name? </TypeWriter>
Is this a known issue or am I using/interpreting the usage of onTypingEnd incorrectly?
Many thanks in advance!
Hi @kirkryan, thanks for the question. It sounds like you want to use multiple instances of TypeWriter
in your component and set each one's typing
prop to 1
after the user is prompted for input. You can use a wrapper component's state to coordinate the multiple instances. You can use the onTypingEnd
callback to track how far along the user is in the steps and set the state to make the next instance of TypeWriter
start typing. Hope that helps!
Actually that’s a great idea! I’d forgotten about using the typing prop and simply used conditional rendering to wrap each Typewriter component then show or hide it based on the progress of the user. Is the onTypingEnd only fired once the complete payload is written out on screen? I believe when I tried using it, it caused a state error
It just fires once when it reaches the end of it's children: https://github.com/TaylorBriggs/react-native-typewriter/blob/master/components/typewriter.js#L108
You have to pass a function to onTypingEnd
. Right now you're passing a call to setState
, which is why you're seeing the stack depth error. It would look something like this:
<TypeWriter typing={1} onTypingEnd={() => this.setState({ hasFinishedTyping: true })} />
Thanks a lot for this lib ! Maybe it can help :
I have had the same issue even with passing a function and not a call to setState as suggested.
Solved it by updating the state only if prevstate was false, since calling set State in function was rerendering the component and calling onTypingEnd again (even though the typing occuring again)
Just to clarify: are you saying the component starts typing again from an empty string on re-render? Once the typewriter component reaches the end of its children it should not start typing again unless its typing
prop changes.
sorry for the typo : the typing IS NOT occuring again
I'm going to close this for now, as it doesn't seem to be a bug in the library itself. Feel free to re-open if you're still having a problem or if you have other questions. Thanks!