假设我有这个:
function Example(props) {
const { prop1, prop2 } = props;
const latestProp1 = useRef(prop1);
useEffect(() => {
latestProp1.current = prop1;
}, [prop1]);
useEffect(() => {
console.log(latestProp1.current);
}, [prop2]);
return null;
}
Run Code Online (Sandbox Code Playgroud)
如果 和prop1在prop2同一个重新渲染中发生变化,是否可以保证控制台会记录新 prop1值?
如果我交换 useEffect 挂钩的位置 -
useEffect(() => {
console.log(latestProp1.current);
}, [prop2]);
useEffect(() => {
latestProp1.current = prop1;
}, [prop1]);
Run Code Online (Sandbox Code Playgroud)
是否可以保证控制台会记录旧 prop1值(来自渲染之前)?
我有一个使用容器#[serde(default)]属性的结构。
但是有一个字段是必需的(如果传入数据中不存在该字段,则解串器应该出错而不是回退到默认值)。
#[serde(default)]
#[derive(Serialize, Deserialize)]
struct Example {
important: i32, // <-- I want this field to be required.
a: i32, // <-- If this field isn't in the incoming data, fallback to the default value.
b: i32, // <-- If this field isn't in the incoming data, fallback to the default value.
c: i32, // <-- If this field isn't in the incoming data, fallback to the default value.
}
Run Code Online (Sandbox Code Playgroud)
编辑:
下面的信息不正确。field属性不采用结构类型的默认值,而是采用每个字段的类型的默认值#[serde(default)] 。(即 …
在结构更新语法中,“扩展”结构必须与生成的结构具有相同的类型。因此,扩展结构必须已经包含所有字段。
那么,还剩下什么没有“用尽”呢?为什么非穷举结构不允许使用结构更新语法?
use some_crate::NonExhaustiveStruct;
let a = NonExhaustiveStruct::default();
let b = {
some_field: true,
..a //Why doesn't this work?
};
Run Code Online (Sandbox Code Playgroud)