-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// FadeInWrapper.js | ||
import { useEffect, useState } from 'react'; | ||
import './FadeInWrapper.css'; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
src/posts/How Do I Use setInterval() For Repeating A Content.html
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/posts/How Do I Use setInterval() For Repeating A String Literal.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<post> | ||
<p>Timing events can be useful when you want to execute the code at specified time intervals.</p> | ||
<p><a target="_blank" href="https://javascript.info/settimeout-setinterval">setInterval()</a> is one of them that allows us to repeatly run a function.</p> | ||
<p>I want to use setInterval() to repeatly render a string content in React:</p> | ||
<pre> | ||
<code class="hljs"><span class="hljs-keyword">const</span> [dots, setDots] = <span class="hljs-variable">useState</span>(<span class="hljs-string">''</span>); | ||
|
||
<span class="hljs-variable">useEffect</span>(() => { | ||
<span class="hljs-keyword">let</span> dotCount = 0; | ||
<span class="hljs-keyword">const</span> interval = <span class="hljs-meta">setInterval</span>(() => { | ||
dotCount = (dotCount + 1) % 4; <span class="hljs-comment">// Reset the count in every 3 strings</span> | ||
<span class="hljs-keyword">let</span> dots = <span class="hljs-string">''</span>; | ||
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = 0; i < dotCount; i++) { | ||
dots += <span class="hljs-string">'.'</span>; | ||
} | ||
<span class="hljs-variable">setDots</span>(dots); | ||
}, 600); | ||
<span class="hljs-keyword">return</span> () => <span class="hljs-meta">clearInterval</span>(interval); | ||
}, []); | ||
</code> | ||
</pre> | ||
<p>You may wanna create a <a target="_blank" href="https://medium.com/@hamzamakh/typewriter-effect-in-react-a103a4f385c9">Typewriter</a> effect by modifying the logic a bit, but it could be tricky here with the React state updates here, the second character is skipped because index is incremented twice before the set state inner function has run once. Consider using a ref to store your string value (updating a reference will not trigger a component re-rendering):</p> | ||
<pre> | ||
<code class="hljs"><span class="hljs-keyword">const</span> [text, setText] = <span class="hljs-variable">useState</span>(<span class="hljs-string">''</span>); | ||
<span class="hljs-keyword">const</span> fullText = <span class="hljs-string">Hello world!</span>; | ||
<span class="hljs-keyword">const</span> index = <span class="hljs-variable">useRef</span>(0); | ||
<span class="hljs-keyword">const</span> displayTextRef = <span class="hljs-variable">useRef</span>(<span class="hljs-string">''</span>); | ||
|
||
<span class="hljs-variable">useEffect</span>(() => { | ||
<span class="hljs-keyword">const</span> interval = <span class="hljs-meta">setInterval</span>(() => { | ||
<span class="hljs-keyword">if</span> (index.current < fullText.length) { | ||
displayTextRef.current += fullText.<span class="hljs-meta">charAt</span>(index.current); | ||
<span class="hljs-variable">setText</span>(() => displayTextRef.current); | ||
index.current += 1; | ||
} <span class="hljs-keyword">else</span> { | ||
<span class="hljs-meta">clearInterval</span>(interval); | ||
} | ||
}, 500); | ||
<span class="hljs-keyword">return</span> () => { | ||
<span class="hljs-variable">setText</span>(<span class="hljs-string">''</span>); | ||
<span class="hljs-meta">clearInterval</span>(interval); | ||
}; | ||
}, []); | ||
</code> | ||
</pre> | ||
<p>This is it!</p> | ||
</post> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters