【发布时间】:2020-03-04 19:46:39
【问题描述】:
我尝试使用 useEffect 但它得到的错误如下所示,
React Hook useEffect has a missing dependency: 'data'. Either include it or remove the dependency array
这是我的组件,
let id = props.location.pathname.split("--")[1];
let str = props.location.pathname.split("--")[0].substr(1);
const data = {id: id, link: str}
const [title, setTitle] = useState("")
useEffect(() => {
setTitle("...") // Yükleniyor.
async function getTitle() {
axios.post('/api/data/entry/get', data)
.then(res => {
setTitle(res.data.title)
// TODO: Catch ekle.
})
}
getTitle()
}, [props])
【问题讨论】:
-
编写代码的最佳方式是将 id 和 str 作为 deps 传递给 useEffect 并在 useEffect 回调中形成数据对象
标签: reactjs react-hooks use-effect