Type ‘UserContextType’不是数组类型。

huangapple go评论69阅读模式
英文:

Type 'UserContextType' is not an array type

问题

Here is the translated content:

我在React中遇到了一些上下文问题。我试图将一个对象设置为状态。在本地工作正常,但当我尝试构建项目时,出现错误,提示:类型 'UserContextType' 不是数组类型。

我不知道为什么会出现这个错误,因为我没有在任何地方尝试将它用作数组。

这是上下文文件:

import React, {
  useState,
  Dispatch,
  SetStateAction,
  createContext,
} from "react";

type Props = {
  children: JSX.Element;
};

type Filters = {
  type: string[];
  rating: number[];
  facilities: string[];
  stars: number[];
  minPrice: number;
  maxPrice: number;
};

type UserContextType = {
  filters: Filters;
  setFilters: Dispatch<SetStateAction<Filters>>;
};

const initialUserContextState = {
  filters: {
    type: [],
    stars: [],
    rating: [],
    facilities: [],
    minPrice: 0,
    maxPrice: 0,
  },
  setFilters: () => {},
};

export const FilterContext = createContext<UserContextType>(initialUserContextState);

export function FilterComponent({ children }: Props) {
  const [filters, setFilters] = useState<Filters>({
    type: [],
    stars: [],
    rating: [],
    facilities: [],
    minPrice: 0,
    maxPrice: 0,
  });
  return (
    <FilterContext.Provider value={{ filters, setFilters }}>
      {children}
    </FilterContext.Provider>
  );
}

export default FilterComponent;

这是我的过滤器组件中使用此状态片段的函数:

function Filter({}: Props) {
  const [filters, setFilters] = useContext(FilterContext);
  const [filteredItems, setFilteredItems] = useState([]);
  const [smallestPrice, setSmallestPrice] = useState<number>();
  const [highestPrice, setHighestPrice] = useState<number>();

  const listFunction = (e: React.FormEvent<HTMLUListElement>) => {
    const listItem = e.target as Element;
    const parentList = listItem.closest(".filter-list__item");

    const filterList = Array.from(
      parentList.querySelectorAll("li.filter__item")
    );

    const newArray = filterList
      .filter((item) => item.querySelector("input:checked"))
      .map((item) => item.querySelector("input").id);

    if (parentList.classList.contains("js-type")) {
      setFilters((prevState) => ({ ...prevState, type: newArray }));
    }

    if (parentList.classList.contains("js-rating")) {
      setFilters((prevState) => ({ ...prevState, rating: newArray }));
    }

    if (parentList.classList.contains("js-facilities")) {
      setFilters((prevState) => ({ ...prevState, facilities: newArray }));
    }
    if (parentList.classList.contains("js-stars")) {
      setFilters((prevState) => ({ ...prevState, stars: newArray }));
    }
  };

  const openFunction = (e: React.MouseEvent<HTMLButtonElement>) => {
    const target = e.target as Element;
    const filterTitle = target.parentNode;
    const filter = filterTitle.parentNode;

    filter.classList.toggle("is-open");
  };
}

非常感谢!

英文:

i'm having some problems with context in React. I'm trying to set an object as state. It works locally but when i try to build the project i get an error saying: Type 'UserContextType' is not an array type.

I don't know why i'm getting this error since i'm not trying to use it as an array anywhere.

Here is the context file:

import React, {
useState,
Dispatch,
SetStateAction,
createContext,
} from &quot;react&quot;;
type Props = {
children: JSX.Element;
};
type Filters = {
type: string[];
rating: number[];
facilities: string[];
stars: number[];
minPrice: number;
maxPrice: number;
};
type UserContextType = {
filters: Filters;
setFilters: Dispatch&lt;SetStateAction&lt;Filters&gt;&gt;;
};
const iUserContextState = {
filters: {
type: [],
stars: [],
rating: [],
facilities: [],
minPrice: 0,
maxPrice: 0,
},
setFilters: () =&gt; {},
};
export const FilterContext = createContext&lt;UserContextType&gt;(iUserContextState);
export function FilterComponent({ children }: Props) {
const [filters, setFilters] = useState&lt;Filters&gt;({
type: [],
stars: [],
rating: [],
facilities: [],
minPrice: 0,
maxPrice: 0,
});
return (
&lt;FilterContext.Provider value={{ filters, setFilters }}&gt;
{children}
&lt;/FilterContext.Provider&gt;
);
}
export default FilterComponent;

This is the function in my filter component where i use this piece of state

function Filter({}: Props) {
const [filters, setFilters] = useContext(FilterContext);
const [filteredItems, setFilteredItems] = useState([]);
const [smallestPrice, setSmallestPrice] = useState&lt;number&gt;();
const [highestPrice, setHighestPrice] = useState&lt;number&gt;();
const listFunction = (e: React.FormEvent&lt;HTMLUListElement&gt;) =&gt; {
const listItem = e.target as Element;
const parentList = listItem.closest(&quot;.filter-list__item&quot;);
const filterList = Array.from(
parentList.querySelectorAll(&quot;li.filter__item&quot;)
);
const newArray = filterList
.filter((item) =&gt; item.querySelector(&quot;input:checked&quot;))
.map((item) =&gt; item.querySelector(&quot;input&quot;).id);
if (parentList.classList.contains(&quot;js-type&quot;)) {
setFilters((prevState) =&gt; ({ ...prevState, type: newArray }));
}
if (parentList.classList.contains(&quot;js-rating&quot;)) {
setFilters((prevState) =&gt; ({ ...prevState, rating: newArray }));
}
if (parentList.classList.contains(&quot;js-facilities&quot;)) {
setFilters((prevState) =&gt; ({ ...prevState, facilities: newArray }));
}
if (parentList.classList.contains(&quot;js-stars&quot;)) {
setFilters((prevState) =&gt; ({ ...prevState, stars: newArray }));
}
};
const openFunction = (e: React.MouseEvent&lt;HTMLButtonElement&gt;) =&gt; {
const target = e.target as Element;
const filterTitle = target.parentNode;
const filter = filterTitle.parentNode;
filter.classList.toggle(&quot;is-open&quot;);
};

Thank you very much!

答案1

得分: 0

- const [filters, setFilters] = useContext(FilterContext);
+ const {filters, setFilters} = useContext(FilterContext);
英文:

change :

 const [filters, setFilters] = useContext(FilterContext);

to :

 const {filters, setFilters} = useContext(FilterContext);

huangapple
  • 本文由 发表于 2023年6月1日 00:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定