我使用的是 react hook 而不是基于类的组件,但是当我从 graphql API 获取数据时它没有更新状态。
这是我的代码:
import React, { useEffect, useState } from 'react';
import client from '../gqlClient';
import { gql, ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: 'http://localhost:8000/graphql/',
cache: new InMemoryCache(),
});
function EmpTable() {
const [employee, setEmployee] = useState({});
useEffect(() => {
client
.query({
query: gql`
query {
employees {
name
}
}
`
})
.then(result => {
setEmployee({result});
console.log(employee);
});
}, [])
return (
<div>return something</div>
)
};
export default EmpTable;
当我打印 employee
时,它只打印初始值。
但是当打印结果时,控制台会显示我从 API 获得的所有数据。
我让 useEffect 只在页面/组件加载后运行,但它不起作用。
谁能帮我解决这个问题?
答案 0 :(得分:3)
setEmployee
是异步方法,因此您无法在 employee
之后立即获取 setEmployee
的更新值。
setEmployee({result});
console.log(employee); // This will show the old `employee` value.
您应该通过添加 useEffect
依赖项在 employee
中获得更新的结果。
useEffect(() => {
client
.query({
query: gql`
query {
employees {
name
}
}
`
})
.then(result => {
setEmployee({result});
console.log(employee);
});
}, [])
useEffect(() => {
console.log(employee);
}, [employee]);
答案 1 :(得分:1)
您需要使用 useEffect 钩子来实现这一点。
有关如何在 React 钩子 How to use `setState` callback on react hooks 上使用 setState
回调的更多信息
这里是你的代码应该是:
import React, { useEffect, useState } from 'react';
import client from '../gqlClient';
import { gql, ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: 'http://localhost:8000/graphql/',
cache: new InMemoryCache(),
});
function EmpTable() {
const [employee, setEmployee] = useState({});
useEffect(() => {
client
.query({
query: gql`
query {
employees {
name
}
}
`
})
.then(result => {
setEmployee({result});
});
}, [])
useEffect(() => {
console.log(employee);
}, [employee]);
return (
<div>{JSON.stringify(employee,null,2)}</div>
)
};
export default EmpTable;
答案 2 :(得分:1)
还有另一种解决方案,您可以使用自定义钩子在您想要使用的任何组件中使用您的返回值。
import React, { useEffect, useState } from 'react';
import client from '../gqlClient';
import { gql, ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
uri: 'http://localhost:8000/graphql/',
cache: new InMemoryCache(),
});
const useCustomApi=()=>{
const [employee, setEmployee] = useState({});
useEffect(() => {
client
.query({
query: gql`
query {
employees {
name
}
}
`
})
.then(result => {
setEmployee({result});
});
}, [])
return employee;
}
function EmpTable() {
const employee = useCustomApi();
console.log(employee);
return (
<div>{JSON.stringify(employee,null,2)}</div>
)
};
export default EmpTable;