英文:
After assign useState values but got undefined?
问题
我在一个 useState
中分配了多个对象,然后使用 Redux Toolkit 从数据库获取数据,然后为这些对象定义了值,但是当我使用 console.log
时,它显示一个状态值未定义,但 console.log
数据库中获取的数据成功。
这是代码
const { data } = useGetConfigQuery();
console.log(data);
console.log(data?.mailgun_api_key);
const configData = {
mailgun_api_key: data?.mailgun_api_key,
mailgun_username: data?.mailgun_username,
mailgun_url: data?.mailgun_url,
email_id_for_mail_sent: data?.email_id_for_mail_sent,
isDiscountAvailable: data?.isDiscountAvailable,
discounted_amount: data?.discounted_amount,
min_amount_scheme_started: data?.min_amount_scheme_started,
max_amount_scheme: data?.max_amount_scheme,
razorpay_key: data?.razorpay_key,
razorpay_secret: data?.razorpay_secret,
razorpay_company_name: data?.razorpay_company_name,
};
const [edit, setEdit] = useState(false);
const [config, setConfig] = useState(configData);
console.log(config.mailgun_api_key);
console.log(config);
const handleChange = (e) => {
setConfig({ ...config, [e.target.name]: e.target.value });
};
Redux 部分
export const configApiSlice = apiSlice.injectEndpoints({
endpoints: (builder) => ({
getConfig: builder.query({
query: () => ({
url: `${CONFIG_API_URL}`,
method: "GET",
}),
keepUnusedDataFor: 5,
providesTags: ["Configuration"],
}),
updateConfig: builder.mutation({
query: (data) => ({
url: `${CONFIG_API_URL}`,
method: "PUT",
body: data,
credentials: "include",
}),
invalidatesTags: ["Configuration"],
}),
}),
});
英文:
I assign multiple objects in one useState
and then fetch the data from the database using the redux toolkit and then defined values for those objects but when I do console.log
it shows me a state value is undefined but console.log
the fetch data from the database is successful.
here is the code
const { data } = useGetConfigQuery();
console.log(data);
console.log(data?.mailgun_api_key);
const configData = {
mailgun_api_key: data?.mailgun_api_key,
mailgun_username: data?.mailgun_username,
mailgun_url: data?.mailgun_url,
email_id_for_mail_sent: data?.email_id_for_mail_sent,
isDiscountAvailable: data?.isDiscountAvailable,
discounted_amount: data?.discounted_amount,
min_amount_scheme_started: data?.min_amount_scheme_started,
max_amount_scheme: data?.max_amount_scheme,
razorpay_key: data?.razorpay_key,
razorpay_secret: data?.razorpay_secret,
razorpay_company_name: data?.razorpay_company_name,
};
const [edit, setEdit] = useState(false);
const [config, setConfig] = useState(configData);
console.log(config.mailgun_api_key);
console.log(config);
const handleChange = (e) => {
setConfig({ ...config, [e.target.name]: e.target.value });
};
the redux part
export const configApiSlice = apiSlice.injectEndpoints({
endpoints: (builder) => ({
getConfig: builder.query({
query: () => ({
url: `${CONFIG_API_URL}`,
method: "GET",
}),
keepUnusedDataFor: 5,
providesTags: ["Configuration"],
}),
updateConfig: builder.mutation({
query: (data) => ({
url: `${CONFIG_API_URL}`,
method: "PUT",
body: data,
credentials: "include",
}),
invalidatesTags: ["Configuration"],
}),
}),
});
答案1
得分: 1
当你最初定义configData时,来自useGetConfigQuery()的数据对象可能尚未填充,因此值将为undefined。
import { useGetConfigQuery } from 'path/to/your/apiSlice'; // 导入查询钩子
const MyComponent = () => {
const { data, isLoading } = useGetConfigQuery(); // 检索数据和加载状态
const configData = {
mailgun_api_key: data?.mailgun_api_key,
mailgun_username: data?.mailgun_username,
// ... 其他属性
};
const [edit, setEdit] = useState(false);
const [config, setConfig] = useState(configData);
useEffect(() => {
// 当数据发生变化时更新config状态
setConfig(configData);
}, [data]);
const handleChange = (e) => {
setConfig({ ...config, [e.target.name]: e.target.value });
};
// 在获取数据时呈现加载状态
if (isLoading) {
return <div>Loading...</div>;
}
console.log(config.mailgun_api_key);
console.log(config);
return (
<div>
{/* 在这里呈现你的组件UI */}
</div>
);
};
export default MyComponent;
英文:
When you initially define configData, the data object from useGetConfigQuery() might not have been populated yet, so the values will be undefined.
import { useGetConfigQuery } from 'path/to/your/apiSlice'; // Import the query hook
const MyComponent = () => {
const { data, isLoading } = useGetConfigQuery(); // Retrieve the data and loading state
const configData = {
mailgun_api_key: data?.mailgun_api_key,
mailgun_username: data?.mailgun_username,
// ... other properties
};
const [edit, setEdit] = useState(false);
const [config, setConfig] = useState(configData);
useEffect(() => {
// Update the config state when the data changes
setConfig(configData);
}, [data]);
const handleChange = (e) => {
setConfig({ ...config, [e.target.name]: e.target.value });
};
// Render loading state while fetching data
if (isLoading) {
return <div>Loading...</div>;
}
console.log(config.mailgun_api_key);
console.log(config);
return (
<div>
{/* Render your component UI here */}
</div>
);
};
export default MyComponent;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论