From 21f91a4f513a3ed8fceeef29de673f0281ca7254 Mon Sep 17 00:00:00 2001 From: Chang Wei Date: Mon, 27 Nov 2023 15:06:20 +0800 Subject: [PATCH] Update freshness.md use the correct way to set a new state if the new state contains a part of old state --- docs/typings/freshness.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/typings/freshness.md b/docs/typings/freshness.md index 065a9a4d..4c4f6eaa 100644 --- a/docs/typings/freshness.md +++ b/docs/typings/freshness.md @@ -80,7 +80,7 @@ interface State { this.setState({ foo: 'Hello' }); // Error: 没有属性 'bar' // 因为 state 包含 'foo' 与 'bar',TypeScript 会强制你这么做: -this.setState({ foo: 'Hello', bar: this.state.bar }); +this.setState((draft) => ({ foo: 'Hello', bar: draft.bar })); ``` 如果你想使用 Freshness,你可能需要将所有成员标记为可选,这仍然会捕捉到拼写错误: