React: Newly Launched useTransition hooks.
Parth Bhoot
|| Results-Driven Business Development Executive || Specializing in IT Services and Market Expansion ||
Hi there,
Here are below snippet shows how to execute the useTransition hook in React.
mport React, { useState, useTransition } from 'react';
const MyComponent = () => {
? const [showContent, setShowContent] = useState(false);
? const [startTransition, isPending] = useTransition({ timeoutMs: 300 });
? const handleClick = () => {
? ? startTransition(() => {
? ? ? setShowContent(!showContent);
? ? });
? };
? return (
? ? <div>
? ? ? <button onClick={handleClick} disabled={isPending}>
? ? ? ? {showContent ? 'Hide Content' : 'Show Content'}
? ? ? </button>
? ? ? {isPending ? (
? ? ? ? <div
? ? ? ? ? style={{
? ? ? ? ? ? width: '100px',
? ? ? ? ? ? height: '100px',
? ? ? ? ? ? backgroundColor: showContent ? 'red' : 'green',
? ? ? ? ? ? transition: 'background-color 0.3s ease-in-out',
? ? ? ? ? }}
? ? ? ? />
? ? ? ) : null}
? ? ? {showContent && <div>This is the content to show or hide.</div>}
? ? ? {!isPending && (
? ? ? ? <button onClick={handleClick} disabled={isPending}>
? ? ? ? ? {showContent ? 'Hide Content' : 'Show Content'}
? ? ? ? </button>
? ? ? )}
? ? </div>
? );
};
export default MyComponent;
领英推荐
Brief Explanation: -
Thanks for reading the post, and looking forward to your valuable insight. We are actively seeking B2B partnership opportunities. If you are interested in collaborating or exploring, please feel free to contact me at [email protected] or DM me. Let's connect together and explore potential growth.
Purvak Pathak MBA MCP