Skip to content

Change sequence and restart typing animation #27

Description

@AlexString

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
	);
};

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions