我如何等待从 useEffect 设置值?
How can i wait for a value to be set from useEffect?
我们在获取数据数组时使用 useMemo 和 useEffect。在第一个文件中,我使用钩子(第二个文件)的结果设置了一个常量。但是结果被设置了两次,第一次结果是一个空对象。
我需要在 useEffect 完成它的工作后设置它。
我们有 2 个 js 文件
- first.js
- second.js
First.js
const dataArray = useGetDataArray({ name, id });
console.log("Data Array: ", dataArray);
Second.js
export const useGetDatArray = ({ name, id } = {}) => {
const [data, setData] = useState({});
const index = useMemo(() => {
console.log("in useMemo");
const client = thirdPartyProvider;
return client;
}, []);
useEffect(() => {
console.log("in useEffect");
index
.search(name, {
numberOfResults: 12,
})
.then(res => setData(_.get(res, "result")));
}, [index]);
return data;
};
我的控制台是这样的
"in useMemo "
"Data Array:" Object { }
"in useEffect"
"Data Array:" Array(12) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
如果可能的话,这是我想要的
"in useMemo"
"in useEffect"
"Data Array:" Array(12) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
First.js
useEffect(async () => {
await useGetDataArray({ name, id });
const dataArray = myContainer.data;
console.log("Data Array: ", dataArray);
}, []);
Second.js
export const useGetDatArray = ({ name, id } = {}) => {
const index = useMemo(() => {
console.log("in useMemo");
const client = thirdPartyProvider;
return client;
}, []);
useEffect(async () => {
console.log("in useEffect");
await myContainer.getData(index, name);
}, [index]);
};
Container.js
function myContainer(){
const [data, setData] = useState({});
const getData = async (index, name) => {
const indexData = await index.search(name, { numberOfResults: 12 });
if (indexData) {
await setData(indexData);
}
}
return { data, getData };
}
export const MyContainer = createContainer(myContainer);
我们在获取数据数组时使用 useMemo 和 useEffect。在第一个文件中,我使用钩子(第二个文件)的结果设置了一个常量。但是结果被设置了两次,第一次结果是一个空对象。
我需要在 useEffect 完成它的工作后设置它。
我们有 2 个 js 文件
- first.js
- second.js
First.js
const dataArray = useGetDataArray({ name, id });
console.log("Data Array: ", dataArray);
Second.js
export const useGetDatArray = ({ name, id } = {}) => {
const [data, setData] = useState({});
const index = useMemo(() => {
console.log("in useMemo");
const client = thirdPartyProvider;
return client;
}, []);
useEffect(() => {
console.log("in useEffect");
index
.search(name, {
numberOfResults: 12,
})
.then(res => setData(_.get(res, "result")));
}, [index]);
return data;
};
我的控制台是这样的
"in useMemo "
"Data Array:" Object { }
"in useEffect"
"Data Array:" Array(12) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
如果可能的话,这是我想要的
"in useMemo"
"in useEffect"
"Data Array:" Array(12) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
First.js
useEffect(async () => {
await useGetDataArray({ name, id });
const dataArray = myContainer.data;
console.log("Data Array: ", dataArray);
}, []);
Second.js
export const useGetDatArray = ({ name, id } = {}) => {
const index = useMemo(() => {
console.log("in useMemo");
const client = thirdPartyProvider;
return client;
}, []);
useEffect(async () => {
console.log("in useEffect");
await myContainer.getData(index, name);
}, [index]);
};
Container.js
function myContainer(){
const [data, setData] = useState({});
const getData = async (index, name) => {
const indexData = await index.search(name, { numberOfResults: 12 });
if (indexData) {
await setData(indexData);
}
}
return { data, getData };
}
export const MyContainer = createContainer(myContainer);