我正在用ReactJS制作MBTI应用程序,但我现在遇到了一些问题
当我单击按钮时,我得到了一些字符串ex 'E或I‘,然后当它完成时,我得到了字符串值ex’‘EEINNSTTFPPJ’,所以我想把这个值更改为'ENTP‘
我怎么才能做到呢?1.状态
const TOTAL_SLIDES = 12
const [score, setScore] = useState(0)
const [type, setType] = useState([])
const [num, setNum] = useState(0)
const [currentSlide, setCurrentSlide] = useState(1)
const slideRef = createRef(null)
const history = useHistory()
const [mbti, setMbti] = useState('')2.funtion
const nextSlideFir = () => {
setNum(num + 1)
setType(questions[num].answers[0].type)
setMbti(mbti + type)
setCurrentSlide(currentSlide + 1)
slideRef.current.style.transform += 'translateX(-100vw)'
}
const nextSlideSec = () => {
setNum(num + 1)
setType(questions[num].answers[1].type)
setMbti(mbti + type)
setCurrentSlide(currentSlide + 1)
slideRef.current.style.transform += 'translateX(-100vw)'
}
//I Don't know how to get same duplicate values
const mbitChecker = string => {
const words = [string]
return words.filter((item, index) => words.indexOf(item) !== index)
}
useEffect(() => {
currentSlide > TOTAL_SLIDES &&
mbitChecker(mbti) &&
history.push(`/result/${mbti}`)
})
)发布于 2021-02-18 12:59:44
不确定您要尝试的是什么,但最好使用useEffect的依赖数组
const extractDuplicates = (text) => {
// extractDuplicates('EEINNSTTFPPJ') -> "ENTP"
const ans = []
for (t of text) {
if (text.indexOf(t) !== text.lastIndexOf(t)) {
if (ans.indexOf(t) < 0) {
ans.push(t)
}
}
}
return ans.join('')
} useEffect(() => {
currentSlide > TOTAL_SLIDES && history.push(`/result/${mbti}`)
console.log(`${mbti}`)
mbitChecker()
}, [mbti])https://stackoverflow.com/questions/66253670
复制相似问题