USEFEFT不想更新我的显示

发布于 2025-02-11 21:56:49 字数 3418 浏览 0 评论 0原文

我是新来的,所以请轻松:-)

我有一种感觉不正确地使用使用效率。基本上,我在页面上有一些广播按钮。当它们更改/单击时,我调用将可变调用状态设置为所选项目的函数。该变量也是使用效果的触发因素,因此我在称为过滤的对象变量上触发并执行一些过滤器。

在控制台中,当我登录它时,它的值中有正确的值,但是屏幕没有更新它们,这是因为我不是在状态下存储该值吗?

这是代码部分:(我将代码修剪为第一次使用变量过滤器)

const [state,setstate] = useState(0)

let projectsList = useSelector(state=>state.projectReducer.projects)
let filteredProject = projectsList

  useEffect(() => {
    if(projectsList) {
      if(state == "All") {
        //show all statuses
        setfilteredProject(projectsList)
      } else{
        //single filter
        filteredProject = projectsList.filter(o => o.state === state);
      }
    }
}, [state])

function toggleStatus(e){
  console.log("state changed");
  setstate(e.value)
}


    return (
      <div className="px-2 py-4 ">
        <div className="card h-fit my-4 lg:w-12/12">
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='All' defaultChecked={true}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' ><b>All Statuses</b> </label></div>
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='Fundraising' defaultChecked={false}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' >Fundrasing </label></div>
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='Successful' defaultChecked={false}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' >Successful </label></div>
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='Expired' defaultChecked={false}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' >Expired </label></div>
        </div>
        <div className=" my-2 lg:my-0 lg:mx-2 grid sm:grid-cols-3 sm:gap-2 grid-cols-1">
          {filteredProject.....

I'm new here so take it easy :-)

I have a feeling I'm using useEffect incorrectly. Basically I have a few radio buttons on the page. When they are change/clicked I call a function that sets a variable call state to the selected item. The variable is also the trigger for the useEffect, so that fires and I perform some filter on an object variable called FilteredProjects.

In the console, when I log this out it has the correct values in them but the screen isn't updating with them, Is this because I'm not storing that value in state?

Here is the code section:(I've trimmed the code to the first use of the variable FilteredProject)

const [state,setstate] = useState(0)

let projectsList = useSelector(state=>state.projectReducer.projects)
let filteredProject = projectsList

  useEffect(() => {
    if(projectsList) {
      if(state == "All") {
        //show all statuses
        setfilteredProject(projectsList)
      } else{
        //single filter
        filteredProject = projectsList.filter(o => o.state === state);
      }
    }
}, [state])

function toggleStatus(e){
  console.log("state changed");
  setstate(e.value)
}


    return (
      <div className="px-2 py-4 ">
        <div className="card h-fit my-4 lg:w-12/12">
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='All' defaultChecked={true}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' ><b>All Statuses</b> </label></div>
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='Fundraising' defaultChecked={false}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' >Fundrasing </label></div>
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='Successful' defaultChecked={false}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' >Successful </label></div>
        <div className='form-check form-check-inline'><input className='form-check-input appearance-none h-4 w-4 border border-gray-300 rounded-sm bg-white checked:bg-blue-600 checked:border-blue-600 focus:outline-none transition duration-200 mt-1 align-top bg-no-repeat bg-center bg-contain float-left mr-2 cursor-pointer' name='status' type='radio' id='status' onClick={(e)=>toggleStatus(e.target)} value='Expired' defaultChecked={false}></input><label className='form-check-label inline-block text-gray-800 font-normal mr-2' >Expired </label></div>
        </div>
        <div className=" my-2 lg:my-0 lg:mx-2 grid sm:grid-cols-3 sm:gap-2 grid-cols-1">
          {filteredProject.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何必那么矫情 2025-02-18 21:56:49
This is worked for me like this, You need to update the state.

const [radioValue, setRadioValue] = useState("male");
const [data, setData] = useState("");

const handleChange = (e) => {
 console.log(e.target.value);
 setRadioValue(e.target.value);
};

useEffect(() => {
if (radioValue === "male") {
  setData("This is Male");
} else {
  setData("This is FeMale");
}
}, [radioValue]);

return (
<div className="App">
  Male
  <input
    name="gender"
    type="radio"
    value="male"
    defaultChecked
    onChange={handleChange}
  />
  Female
  <input
    name="gender"
    type="radio"
    value="female"
    onChange={handleChange}
  />
  <div>
    <h3>{data}</h3>
  </div>
</div>
)
This is worked for me like this, You need to update the state.

const [radioValue, setRadioValue] = useState("male");
const [data, setData] = useState("");

const handleChange = (e) => {
 console.log(e.target.value);
 setRadioValue(e.target.value);
};

useEffect(() => {
if (radioValue === "male") {
  setData("This is Male");
} else {
  setData("This is FeMale");
}
}, [radioValue]);

return (
<div className="App">
  Male
  <input
    name="gender"
    type="radio"
    value="male"
    defaultChecked
    onChange={handleChange}
  />
  Female
  <input
    name="gender"
    type="radio"
    value="female"
    onChange={handleChange}
  />
  <div>
    <h3>{data}</h3>
  </div>
</div>
)
难如初 2025-02-18 21:56:49

当值更改屏幕时,您应该尝试将过滤的备件保存为状态,并应更改显示屏。

you should try to save filteredProject as state that way when the value change the screen will rerender and should change the display.

感性不性感 2025-02-18 21:56:49

您可以使用这样的使用效率,并且可以更新状态:

const ButtonUpdate = () => {
  const [count, setCount] = useState(0)
  const [name, setName] = useState('om')

  useEffect(() => {
    console.log(`Hi ${name} you clicked ${count} times`)
  });
  
  return (
    <div>
      <p>Hi {name} you clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
      <button onClick={() => setName(name === 'om' ? 'krishn' : 'Boldo`)}>
        Change name
      </button>
    </div>
  )
}

You can use useEffect like this and you can update state :

const ButtonUpdate = () => {
  const [count, setCount] = useState(0)
  const [name, setName] = useState('om')

  useEffect(() => {
    console.log(`Hi ${name} you clicked ${count} times`)
  });
  
  return (
    <div>
      <p>Hi {name} you clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
      <button onClick={() => setName(name === 'om' ? 'krishn' : 'Boldo`)}>
        Change name
      </button>
    </div>
  )
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文