An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 15, 2022 12:47 am GMT
{placeholder}
)
Original Link: https://dev.to/bay_area_ninja/help-useref-within-useeffect-468o
help useRef within useEffect
my Goal: Is to create a simple effect that prints out a string one letter at a time.
example: string = "TEXT". it should print T E X T
Whats currently being printed is: T X T undefined
my code:
`const [placeholder, setPlaceholder] = useState('');
const
string = 'TEXT',
index = useRef(0);
useEffect(() => {
function showChar() {
setPlaceholder(prev => prev + string[index.current]);
console.log(index.current)
index.current++;
}
if (index.current < string.length) {
let addChar = setInterval(showChar, 500);
return () => clearInterval(addChar);
}
}, [placeholder]);
return (
{placeholder}
)
export default App;`
Original Link: https://dev.to/bay_area_ninja/help-useref-within-useeffect-468o
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To