import { useEffect, useRef } from "react"; import { useDispatch, useSelector } from "react-redux"; import { setRef, setCaretRef } from "store/actions"; import { State } from "store/reducer"; import "stylesheets/Test.scss"; export default function Test() { const { word: { typedWord, currWord, wordList, typedHistory }, time: { timer }, } = useSelector((state: State) => state); const dispatch = useDispatch(); const extraLetters = typedWord.slice(currWord.length).split(""); const activeWord = useRef(null); const caretRef = useRef(null); useEffect(() => { dispatch(setRef(activeWord)); dispatch(setCaretRef(caretRef)); }, [dispatch]); return (
{timer}
{wordList.map((word, idx) => { const isActive = currWord === word && typedHistory.length === idx; return (
{isActive ? ( | ) : null} {word.split("").map((char, charId) => { return {char}; })} {isActive ? extraLetters.map((char, charId) => { return ( {char} ); }) : typedHistory[idx] ? typedHistory[idx] .slice(wordList[idx].length) .split("") .map((char, charId) => { return ( {char} ); }) : null}
); })}
); }