gpt4 book ai didi

javascript - 我的状态没有使用 React Hooks 更新

转载 作者:行者123 更新时间:2023-12-04 09:15:04 24 4
gpt4 key购买 nike

我有一个事件会在更改时触发。此事件检查哪个过滤器已更改,然后相应地更新状态。
如果我不使用 react 钩子(Hook),我会选择这样的 -

const target = e.target;
const value = e.type === "checkbox" ? target.checked : target.value;
const name = e.target.name;
this.setState({ [name]: value });
如您所见,我可以使用 [name] 更新州名。但是由于我使用的是 react 钩子(Hook),所以我无法使用上述内容。相反,我得到了这个-
  const [type, setType] = useState("all");
const [capacity, setCapacity] = useState(1);
const [price, setPrice] = useState(0);
const [minPrice, setMinPrice] = useState(0);
const [maxPrice, setMaxPrice] = useState(0);
const [minSize, setMinSize] = useState(0);
const [maxSize, setMaxSize] = useState(0);

const handleChange = (e) => {
const target = e.target;
const value = e.type === "checkbox" ? target.checked : target.value;
const name = e.target.name;

switch (name) {
case "type":
setType(value);
break;
case "capacity":
setCapacity(value);
break;
case "price":
setPrice(value);
break;
case "minPrice":
setMinPrice(value);
break;
case "maxPrice":
setMaxPrice(value);
break;
case "minSize":
setMinSize(value);
break;
case "maxSize":
setMaxSize(value);
break;
default:
return;
}
};
您在上面看到的内容由于某种原因不起作用。当我触发过滤器时,它会进入我已经测试过的情况,但状态似乎没有更新。名称和值也已记录,它们也很好。我不确定是什么原因造成的,我只是编程新手,所以任何帮助都将不胜感激!
*** 只是重申值和名称都有效,switch 语句也是如此。只是没有更新的状态***

最佳答案

我的问题的解决方法是它没有重新渲染,所以它是整个时间的一个循环。
在 useEffect 中添加状态作为依赖项可以解决问题!我希望这可以帮助其他有类似问题的人,以某种方式帮助社区会很棒。

import React, { createContext, useState, useEffect } from "react";

import items from "../data";

export const RoomContext = createContext();

const RoomProvider = (props) => {
const [rooms, setRooms] = useState([]);
const [sortedRooms, setSortedRooms] = useState([]);
const [featuredRooms, setFeaturedRooms] = useState([]);
const [loading, setLoading] = useState(true);
const [type, setType] = useState("all");
const [capacity, setCapacity] = useState(1);
const [price, setPrice] = useState(0);
const [minPrice, setMinPrice] = useState(0);
const [maxPrice, setMaxPrice] = useState(0);
const [minSize, setMinSize] = useState(0);
const [maxSize, setMaxSize] = useState(0);
const [breakfast, setBreakfast] = useState(false);
const [pets, setPets] = useState(false);

//call the formatted data
useEffect(() => {
//set rooms
const rooms = formatData(items);
//set the featured rooms
const featuredRooms = rooms.filter((room) => room.featured === true);
//always show the max price as default
const maxPrice = Math.max(...rooms.map((item) => item.price));
//always show the max price as default
const maxSize = Math.max(...rooms.map((item) => item.size));

setRooms(rooms);
setSortedRooms(rooms);
setFeaturedRooms(featuredRooms);
setLoading(false);
setMaxPrice(maxPrice);
setMaxSize(maxSize);
setPrice(maxPrice);
}, [
type,
capacity,
price,
minPrice,
maxPrice,
minSize,
maxSize,
breakfast,
pets,
]);

//format data is getting all the required fields we want to pull from the originl data
const formatData = (items) => {
let tempItems = items.map((item) => {
const id = item.sys.id;
const images = item.fields.images.map((image) => image.fields.file.url);

const room = { ...item.fields, images: images, id: id };
return room;
});
return tempItems;
};

const getRoom = (slug) => {
let tempRooms = [...rooms];
const room = tempRooms.find((room) => room.slug === slug);
return room;
};

const handleChange = (e) => {
const target = e.target;
const value = e.type === "checkbox" ? target.checked : target.value;
const name = e.target.name;

switch (name) {
case "type":
setType(value);
break;
case "capacity":
setCapacity(value);
break;
case "price":
setPrice(value);
break;
case "minPrice":
setMinPrice(value);
break;
case "maxPrice":
setMaxPrice(value);
break;
case "minSize":
setMinSize(value);
break;
case "maxSize":
setMaxSize(value);
break;
case "breakfast":
setBreakfast(value);
break;
case "pets":
setPets(value);
break;
default:
return;
}
console.log(name, value);
};

const filteredRooms = () => {
let initialRooms = [...rooms];

if (type !== "all") {
initialRooms = initialRooms.filter((room) => room.type === type);
}
console.log("room type is", type);
setSortedRooms(initialRooms);
};

return (
<RoomContext.Provider
value={{
rooms,
featuredRooms,
getRoom,
loading,
sortedRooms,
type,
capacity,
price,
minPrice,
maxPrice,
minSize,
maxSize,
breakfast,
pets,
handleChange,
filteredRooms,
}}
>
{props.children}
</RoomContext.Provider>
);
};

export default RoomProvider;

关于javascript - 我的状态没有使用 React Hooks 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63261089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com