英文:
React make one component wait for another
问题
我想要一个组件的实例在另一个相同组件的实例在不同的父组件中执行完成后被调用。
我有一个名为TypeWriter的组件,它显示类型动画,每个字符之间有延迟。
const TypeWriter = ({text,delay}) => {
const [currentTxt, setCurrentText] = useState('');
const [currentIdx, setCurrentIdx] = useState(0);
useEffect(() => {
let timeOut;
if(text.length > currentIdx) {
timeOut = setTimeout(() => {
setCurrentText(currentTxt => currentTxt + text[currentIdx]);
setCurrentIdx(currentIdx => currentIdx + 1);
}, delay);
}
return () => clearTimeout(timeOut);
}, [currentIdx]);
return <>{currentTxt}</>;
}
问题是我希望一个TypeWriter实例在前一个完成后被调用(useEffect结束):
const SomeComponent = () => (
<h1 className={`${styles.heroHeadText} lg:leading-normal -md:text-xl -md:mt-5`}>
<TypeWriter text="Hello, it's me " delay={200} />
<span className='text-[#915eff]'>
<TypeWriter text=" name" delay={200} />
</span>
</h1>
)
span中的TypeWriter必须在h1中的TypeWriter完成后被实例化。
我是React的新手,请让我知道我是否可以在实例之间使用mutexLock。提前感谢(:
英文:
I want one instance of component to be called after other instance of same component completed with its execution inside a different parent component
I have a component TypeWriter that shows type animation with delay between each character being written
const TypeWriter = ({text,delay})=>{
const [currentTxt, setCurrentText] = useState('');
const [currentIdx,setCurrentIdx] = useState(0);
useEffect(()=>{
let timeOut;
if(text.length>currentIdx){
timeOut = setTimeout(()=>{
setCurrentText(currentTxt=>currentTxt+text[currentIdx]);
setCurrentIdx(currentIdx=>currentIdx+1);
},delay)
}
return ()=>clearTimeout(timeOut);
},[currentIdx])
return <>{currentTxt}</>
}
Issue is I want one TypeWriter instance to be called after previous one complete (useEffect end):
const SomeComponent = ()=>return (<h1 className={`${styles.heroHeadText} lg:leading-normal -md:text-xl -md:mt-5`}>
<TypeWriter text="Hello, it's me " delay={200}/>
<span className='text-[#915eff]'><TypeWriter text=" name" delay={200}/></span>
</h1>)
TypeWriter inside span must be instantiated after TypeWriter inside h1 is completed
I am new to React, let me know if in anyone I can use mutexLock between instances.
Thanks in advance (:
答案1
得分: 1
以下是翻译好的部分:
最简单的解决方案我能想到的是一个回调函数,可以传递给你的TypeWriter组件。
这样,在你的SomeComponent
中,你可以初始化一个状态来跟踪你的第一个TypeWriter的工作。一旦第一个打字机完成了它的工作,回调函数会更新你的第一个打字机实例的状态。
然后,一旦第一个完成,你可以有条件地渲染第二个实例。类似这样:
const TypeWriter = ({text,delay,onComplete}) => {
const [currentTxt, setCurrentText] = useState('');
const [currentIdx,setCurrentIdx] = useState(0);
useEffect(() => {
let timeOut;
if(text.length > currentIdx){
timeOut = setTimeout(() => {
setCurrentText(currentTxt => currentTxt + text[currentIdx]);
setCurrentIdx(currentIdx => currentIdx + 1);
}, delay)
}
if(onComplete) {
onComplete();
}
return () => clearTimeout(timeOut);
}, [currentIdx])
return <>{currentTxt}</>;
}
然后像这样有条件地渲染第二个实例。
const SomeComponent = () => {
const [firstTextComplete, setFirstTextComplete] = useState(false);
return (
<TypeWriter text="Hello, it's me " delay={200} onComplete={() => setFirstTextComplete(true)}/>
{firstTextComplete && <span className='text-[#915eff]'><TypeWriter text=" name" delay={200}/></span>}
);
};
英文:
The simplest solution I can think of is a callback function that can be passed into your TypeWriter component.
This way in your SomeComponent
, you can initialise a state to keep track of your first TypeWriter work. Once the first typewriter has finished its job, the callback function updates the state of your first instance of typewriter.
Then you can conditionally render your second instance once the first has finished. Something like this
const TypeWriter = ({text,delay,onComplete})=>{
const [currentTxt, setCurrentText] = useState('');
const [currentIdx,setCurrentIdx] = useState(0);
useEffect(()=>{
let timeOut;
if(text.length>currentIdx){
timeOut = setTimeout(()=>{
setCurrentText(currentTxt=>currentTxt+text[currentIdx]);
setCurrentIdx(currentIdx=>currentIdx+1);
},delay)
}
if(onComplete) {
onComplete();
}
return ()=>clearTimeout(timeOut);
},[currentIdx])
return <>{currentTxt}</>
}
Then conditionally render the second instance like this.
const SomeComponent = () => {
const [firstTextComplete, setFirstTextComplete] = useState(false);
return (
<TypeWriter text="Hello, it's me " delay={200} onComplete={() => setFirstTextComplete(true)}/>
{firstTextComplete && <span className='text-[#915eff]'><TypeWriter text=" name" delay={200}/></span>}
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论