[英]array is cleared when its function is called with another function in an onclick attribute
I have two parent divs each of them has 5 child divs, onClick on one of the child divs a function that changes the class name is called and onDoubleClick on the child div a function is called to pop the last element from an array and push a certain value.我有两个父 div,每个 div 有 5 个子 div,在其中一个子 div 上单击 onClick 调用更改类名的函数,在子 div 上调用 onDoubleClick 函数以从数组中弹出最后一个元素并推送一定的价值。
then a condition is executed on the values of the two array.然后对两个数组的值执行条件。 the problem is the first array always sent empty to the condition but in case of I deleted the part on the onClick function, the array is sent correctly to the condition.问题是第一个数组总是向条件发送空但如果我删除了 onClick 函数上的部分,则数组被正确发送到条件。
what could be the problem.可能是什么问题呢。
import React, { Component, useEffect, useState } from "react";
const Survey = () => {
const array1 = [];
const array2 = [];
const [classA, setClassA] = useState();
const handleClassA = (id) => {
setClassA(id);
}
const handleArray1 = (id) => {
array1.pop();
array1.push(id);
console.log('array1', array1);
}
const [classB, setClassB] = useState();
const handleClassB = (id) => {
setClassB(id);
}
const handleArray2 = (id) => {
array2.pop();
array2.push(id);
console.log('array2', array2);
}
const Quiz = () => {
if (array1.includes(0) || array1.includes(1) || array1.includes(2)) {
console.log('one is 0,1,2');
if (array2.includes(0) || array2.includes(1) || array2.includes(2)) {
console.log('two is 0,1,2');
} else if (array2.includes(3) || array2.includes(4)) {
console.log('counseling');
setResult("Counseling2");
}
} else if (array1.includes(3) || array1.includes(4)) {
console.log('one is 3,4');
if (array2.includes(0) || array2.includes(1) || array2.includes(2)) {
console.log('two is 0, 1,2');
}
} else if (array2.includes(3) || array2.includes(4)) {
console.log('two is 3,4');
console.log('therapy');
setResult("Therapy")
}
}
console.log('quiz done');
console.log(array1);
console.log(array2);
}
return(
<div className="survey_questions">
<div className="survey_q_1">
<p id="q_1">I’m concerned about a behavior, feeling, or something I’m doing.</p>
<div className="survey_a_1">
<div
className={classA === 0 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassA(0);
}}
onDoubleClick={() => { handleArray1(0) }}
>
not at all
</div>
<div
className={classA === 1 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassA(1);
}}
onDoubleClick={() => { handleArray1(1) }}
>
just a little
</div>
<div
className={classA === 2 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassA(2);
}}
onDoubleClick={() => { handleArray1(2) }}
>
moderately
</div>
<div
className={classA === 3 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassA(3);
}}
onDoubleClick={() => { handleArray1(3) }}
>
quite a lot
</div>
<div
className={classA === 4 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassA(4);
}}
onDoubleClick={() => { handleArray1(4) }}
>
very much
</div>
</div>
</div >
<div className="survey_q_1">
<p id="q_1">This behavior or feeling has been getting worse in the past few weeks.</p>
<div className="survey_a_1">
<div
className={classB === 5 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassB(5);
}}
onDoubleClick={() => { handleArray2(0) }}>
not at all
</div>
<div
className={classB === 6 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassB(6);
}}
onDoubleClick={() => { handleArray2(1) }}>
just a little
</div>
<div
className={classB === 7 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassB(7);
}}
onDoubleClick={() => { handleArray2(2) }}>
moderately
</div>
<div
className={classB === 8 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassB(8);
}}
onDoubleClick={() => { handleArray2(3) }}>
quite a lot
</div>
<div
className={classB === 9 ? "survey_a_active" : "survey_a"}
id="a_1"
onClick={() => {
handleClassB(9);
}}
onDoubleClick={() => { handleArray2(4) }}>
very much
</div>
</div>
</div >
</div>
)
}
export default Survey;
React Functional component will execute the component(function) itself every time it re-render. React Functional 组件在每次重新渲染时都会自行执行组件(函数)。
In your case, your array1 and array 2 are not a state, every time component re-render, your array will be [ ] as you declare it.在您的情况下,您的 array1 和 array 2 不是一个状态,每次组件重新渲染时,您的数组将是 [ ] ,因为您声明它。
const array1 = [];
const array2 = [];
wrap it into state as:将其包装成以下状态:
const [array1, setArray1] = useState([]);
const [array2, setArray2] = useState([]);
I solved the problem by using useState, thanks to all who helped me.我通过使用 useState 解决了这个问题,感谢所有帮助我的人。
const [array1, setArray1] = useState([]);
const [array2, setArray2] = useState([]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.