USEFEFT不想更新我的显示
我是新来的,所以请轻松:-)
我有一种感觉不正确地使用使用效率。基本上,我在页面上有一些广播按钮。当它们更改/单击时,我调用将可变调用状态设置为所选项目的函数。该变量也是使用效果的触发因素,因此我在称为过滤的对象变量上触发并执行一些过滤器。
在控制台中,当我登录它时,它的值中有正确的值,但是屏幕没有更新它们,这是因为我不是在状态下存储该值吗?
这是代码部分:(我将代码修剪为第一次使用变量过滤器)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当值更改屏幕时,您应该尝试将过滤的备件保存为状态,并应更改显示屏。
you should try to save filteredProject as state that way when the value change the screen will rerender and should change the display.
您可以使用这样的使用效率,并且可以更新状态:
You can use useEffect like this and you can update state :