Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change sequence and restart typing animation #27

Open
AlexString opened this issue Jun 6, 2023 · 4 comments
Open

Change sequence and restart typing animation #27

AlexString opened this issue Jun 6, 2023 · 4 comments

Comments

@AlexString
Copy link

AlexString commented Jun 6, 2023

As it is mentioned in the Usage Notes

Immutability
Due to the nature of the animation, this component is permanently memoized, which means that the component never re-renders unless you hard-reload the page, and hence props changes will not be reflected.

I'm using this nice package in a website where switching between languages is involved so I needed to change sequence on language change.

I made this workaround to achieve this and I hope this helps someone who is looking for something similar.

function useOnChangeSequence(sequence: any[]) {
	const [animationFlag, setAnimationFlag] = useState(false);

	// Put down flag when sequence changed
	useEffect(() => setAnimationFlag(false), [sequence]);

	useEffect(() => {
		if (animationFlag) return; // Avoid infinite loop
		setAnimationFlag(true); // raise flag to trigger animation
	}, [animationFlag]);

	return animationFlag;
}

And I'm using it like so:

const TypeWriterText: FC<ITypeWriterTextProps> = ({
	sequence,
	wrapper = 'p',
	onFinish,
	cursor = true,
	className = '',
}) => {
	const animationFlag = useOnChangeSequence(sequence);
	return (
		animationFlag ?
			<TypeAnimation
				cursor={cursor}
				sequence={sequence}
				speed={30}
				wrapper={wrapper}
				className={className}
			/>
			: null
	);
};
@karelric
Copy link

I agree, the component is awesome but should react to sequence changes and re-render the components.

@ihorml
Copy link

ihorml commented Oct 20, 2023

@AlexString thanks for the workaround!

@htranq
Copy link

htranq commented Dec 13, 2023

I thinks we can using the key prop for remount component, just naming value for each language code, that works for me.

             return (<TypeAnimation
                                key={lang} // lang is current language
				cursor={cursor}
				sequence={sequence}
				speed={30}
				wrapper={wrapper}
				className={className}
			/>)

@maxeth may you update docs at Hot Reload not supported ? for how to remount the component, thanks you.

@izcoser
Copy link

izcoser commented Apr 28, 2024

@htranq what is the difference between this method vs the OP method? Certainly yours is cleaner but I wonder if there's any performance difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants