reactjs Uncaught(in promise)TypeError:setExercises不是函数

2uluyalo  于 2023-05-17  发布在  React
关注(0)|答案(2)|浏览(102)

我做了一个Recat健身房网站使用API,在这个当我搜索的东西,它得到的数据,并显示错误即,数据不传递到setExercise显示它到屏幕上。这是用于搜索onClick={handlesearch}的函数:

const SearchExercises = (setExercises) => {

const handleSearch= async()=>{
            if(search){
                const exerciseData = await fetchData('https://exercisedb.p.rapidapi.com/exercises', exerciseOptions);
                const SearchExercises = exerciseData.filter((exercise)=>
                    exercise.name.toLowerCase().includes(search));
                setSearch('');
                setExercises(SearchExercises);
            }
        }
}

useState在home.js文件中定义

function Home() {
  const [bodyPart,setBodyPart] = useState('all')
  const [exercises, setExercises] = useState([])
  return (
    <Box><SearchExercises setExercises={setExercises} bodyPart={bodyPart} setBodyPart={setBodyPart}/>
    </Box>
  )
}
j91ykkif

j91ykkif1#

当你向一个React组件传递props时,第一个参数包含了你所有的props。常见的模式是像这样破坏它们

const SearchExercises = ({ setExercises, bodyPart, setBodyPart }) => {
1yjd4xko

1yjd4xko2#

你应该得到这样的 prop 。

const SearchExercises = ({setExercises}) => {

...

相关问题