【发布时间】:2020-02-10 15:59:01
【问题描述】:
我有一个反应代码
- 设置
empty state -
fills国家 - 一旦这个状态被填充,它
renders一些图像 - 然后此图像会触发
onLoad事件 - 这个 onLoad 事件然后调用
function那个reads初始state - 但是这个状态是
empty
这怎么可能?如果函数被调用,则表示状态不再为空
笔https://codesandbox.io/s/usestate-strange-things-9rydu
代码
import React, { useRef, useState, useEffect } from "react";
import styled from "@emotion/styled";
const useMyHook = (virtual_structure, setVirtual_structure) => {
useEffect(() => {
console.log("virtual_structure is updated!");
console.log(virtual_structure);
console.log("____virtual_structure is updated!");
}, [virtual_structure]);
const refs = useRef([]);
const createStructure = () => {
console.log("virtual_structure, is it empty?");
console.log(virtual_structure);
};
useEffect(() => {
createStructure();
}, []);
const assignRef = r =>
r && (refs.current.includes(r) || refs.current.push(r));
return [assignRef, createStructure];
};
export default function App() {
const [virtual_structure, setVirtual_structure] = useState([]);
const [assignRef, updateGrid] = useMyHook(
virtual_structure,
setVirtual_structure
);
useEffect(() => {
const temp_structure = Array.from({ length: 4 }, () => ({
height: 0,
cells: []
}));
temp_structure[0].cells = Array.from({ length: 10 }, () => {
const rand = Math.random();
const r = rand > 0.1 ? parseInt(500 * rand) : parseInt(500 * 0.1);
return {
height: "",
el: (
<div ref={assignRef}>
<Image
alt=""
onload={updateGrid}
num=""
src={`https://picsum.photos/200/${r}`}
/>
</div>
)
};
});
setVirtual_structure(temp_structure);
}, []);
return (
<Container>
{virtual_structure.map((col, i) => (
<div key={`col${i}`}>
{col.cells && col.cells.map((cell, j) => <>{cell.el}</>)}
</div>
))}
</Container>
);
}
const Image = ({ alt, onload, num, src }) => (
<>
<Label>{num}</Label>
<Img src={src} alt={alt} onLoad={onload} />
</>
);
const Img = styled.img`
border: 1px solid #000;
height: min-content;
margin: 0;
padding: 0;
`;
const Label = styled.div`
position: absolute;
`;
const Container = styled.div`
width: 100%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
background: #ccc;
align-content: center;
div {
flex: 1;
div {
color: #fff;
font-weight: 700;
font-size: 32px;
margin: 4px;
}
}
`;
还有console.log
virtual_structure is updated!
index.js:27 Array(0)length: 0__proto__: Array(0)
index.js:27 ____virtual_structure is updated!
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure is updated!
index.js:27 Array(4)0: {height: 0, cells: Array(10)}1: {height: 0, cells: Array(0)}2: {height: 0, cells: Array(0)}3: {height: 0, cells: Array(0)}length: 4__proto__: Array(0)
index.js:27 ____virtual_structure is updated!
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 Array(0)length: 0__proto__: Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 []length: 0__proto__: Array(0)
index.js:27 virtual_structure, is it empty?
index.js:27 []
【问题讨论】:
-
您需要将您在挂钩中使用的所有变量传递到挂钩的第二个参数的数组中。例如。
useHook(()=>{...},[assignRef, updateGrid, setVirtual_structure]。如果不这样做,将使您的变量过时并产生许多其他难以调试的错误 -
您能否提供一个示例或指向一些文档?这种语法我认为它是 useEffect 等人独有的
-
确实是关于
useEffect。我说 useHooks 是为了让你也注意你自己创建的钩子!
标签: javascript reactjs react-hooks react-state