reactjs Redux Toolkit中状态实现的问题

ijnw1ujt  于 2023-08-04  发布在  React
关注(0)|答案(1)|浏览(116)

我在更新toolkit中的状态时遇到了一些(可能是愚蠢的)问题。
我在完成React课程后正在做我的第一个主要项目,我在markAsKnown函数中更新状态时遇到了一个问题,我希望它在第一次单击state.maxIndex = state.technologyArray.length后立即获得更新的状态,并且在第二次单击后发生。
你可以在屏幕截图上看到,在第一次点击markAsKnown调用后,没有任何变化,只有在第二次点击时才有更新,但这会破坏一切,因为一个是我想在页面上显示technologyArray的长度,它会通知剩余的问题,另一个是它会影响索引绘制,这样做可能会比数组的长度更长。
这是我的Slice:

import { createSlice } from '@reduxjs/toolkit';
import QuestionsData from '@/data/QuestionsData';

const initialState = {
maxIndex: 10, // chwilowo
index: 0,
questions: QuestionsData,
technology: 'JavaScript',
technologyArray: [],
};

const questionSlice = createSlice({
name: 'question',
initialState,

reducers: {
getRandomIndex: (state) => {
  state.index = Math.floor(Math.random() * state.maxIndex);
},

getTechnologyArray: (state, action) => {
  state.technologyArray = state.questions.filter(
    (tech) => action.payload === tech.tech && tech.known === false
  );
  state.maxIndex = state.technologyArray.length;
  state.technology = action.payload;
  state.index = 0;
},

markAsKnown: (state) => {
  state.technologyArray = state.questions.filter(
    (tech) => state.technology === tech.tech && tech.known === false
  );
  state.maxIndex = state.technologyArray.length;
  state.technologyArray[state.index].known = true;
  if (state.maxIndex === 0) console.log('Koniec pytań');
},

resetCategory: (state) => {
  state.questions = QuestionsData;
},
},
});

export const {
markAsKnown,
getRandomIndex,
index,
getTechnologyArray,
technologyArray,
} = questionSlice.actions;

export default questionSlice.reducer;

字符串
`
在这里我称之为:

import { BsCheckCircle, BsXCircle } from 'react-icons/bs';
import { useDispatch } from 'react-redux';

const KnownUnknownButtons = ({ markAsKnown, getRandomIndex }) => {
const dispatch = useDispatch();

return (
<div className="max-w-7xl w-full flex flex-col justify-between items-center mx-auto p-5 ">
  <div className="flex mt-10 flex justify-around w-full sm:w-4/5 md:w-3/5 lg:w-1/2 ">
    <button className=" flex justify-center">
      <BsXCircle
        onClick={() => {
          dispatch(getRandomIndex());
        }}
        className="h-14 w-14 hover:text-red-500 transition-all hover:scale-105 active:scale-95"
      />
    </button>
    <button className=" flex justify-center">
      <BsCheckCircle
        onClick={() => {
          dispatch(getRandomIndex());
          dispatch(markAsKnown());
        }}
        className="h-14 w-14 hover:text-teal-500 transition-all hover:scale-105 active:scale-95"
      />
    </button>
  </div>
</div>
);
};
export default KnownUnknownButtons;


`
我试过将状态放入变量中并将其分配给状态,重新排序代码,我甚至试过用gpt chat修复它,但我找不到解决方案。

bihw5rsg

bihw5rsg1#

看起来这个问题可能与点击按钮后更新Redux状态有关。在markAsKnown reducer中修改technologyArray和maxIndex时,请考虑使用函数更新方法。这可能有助于确保在第一次单击后立即更新状态。
举例来说:

markAsKnown: (state) => {
  const newTechArray = 
  state.technologyArray.map((tech, idx) => 
  {
    return (idx === state.index) ? { 
    ...tech, known: true } : tech;
   });
  state.technologyArray = newTechArray;
  state.maxIndex = 
  newTechArray.filter(tech => 
  !tech.known).length;
  if (state.maxIndex === 0) 
  console.log('End of questions');
 },

字符串

相关问题