function QuestionAns(){
const url = 'http://127.0.0.1:8000/'
const [data, setData] = useState([]);
const [currData, setCurrData] = useState([])
let i = 1
useEffect(() => {
let result = []
const fetchFromUrl = async() => {
const response = await axios.get(url)
const response_data = await response.data.data
await setData(response_data)
}
fetchFromUrl()
}, [])
const run_loop = async ()=>{
let arr = []
for(let counter = 0; counter < 12; counter++){
arr.push(<Pretty count={i} question={data[i].question} answer_option={data[i].option_arr} answer={data[i].answer} key={data[i]._id} corr_ans={data[i]._id}/>)
i+=1
}
let dat = await arr.concat(currData)
await setCurrData(dat)
}
if(data.length){
run_loop()
}
return (
<div className="container my-3">
<div className="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-5">
{data.length ? currData : `loading`}
</div>
</div>
)
}
export default QuestionAns;
In this function fetchFromUrl we are getting the data from the server which is stored in the data state again I am taking 12 data from the fetched content and storing it to currData. Purpose is to make a infinite scroll. but if I keep the code like that I am getting a infinite entries of 12 data means 1 to 12 -> 1 to 12 -> 1 to 12 ..... . Problem lies with currData.
Any idea why I am getting in this infinite loop. I am good with raw js and can do this this easily but I am learning react now so any help will be appriciated.
I was expecting the data from 1-12 will load single time.
