【发布时间】:2020-12-15 15:03:54
【问题描述】:
我有一个组件,就是句柄输入组件
import React, {useState} from 'react';
import FacebookApiCall from './Api'
export default function FacebookInterest () {
const [interest, setInterest] = useState('');
function HandleChange(event) {
setInterest(event.target.value);
}
return (
<div className="maincontainer">
<input className="searchbar" type="text" placeholder="Type an interest ..." value={interest} onChange={HandleChange}/>
<button onClick={() => FacebookApiCall(interest)} className="searchbutton">Search</button>
</div>
);
}
当用户点击时,我想通过 FacebookApiCall 调用 API
import React, {useEffect} from 'react'
import axios from 'axios'
export default function FacebookApiCall(props) {
const url = `https://graph.facebook.com/search?type=adinterest&q=${props}&limit=10000&locale=en_US&access_token=EA`
useEffect(() => {
axios.get(url)
.then(res => {
console.log(url);
console.log(res);
//console.log(res.data.data[62].audience_size);
//console.log(res.data.data[62].name);
//console.log(res.data.data[62].description);
//console.log(res.data.data[62].path);
})
});
return (null);
}
react 返回的错误是:
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
- 您可能有不匹配的 React 版本和渲染器(例如 React DOM)
- 您可能违反了 Hooks 规则
- 您可能在同一个应用中拥有多个 React 副本
所以我不能在我的 Facebook 函数中使用钩子,我知道现在我如何在不使用 useEffect 的情况下进行 api 调用,如果无论如何我都必须使用 useEffect,我应该怎么做?我不得不承认我在这里迷路了。
非常感谢大家
【问题讨论】:
标签: reactjs axios react-hooks