Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ascoders/weekly
Browse files Browse the repository at this point in the history
  • Loading branch information
ascoders committed Dec 25, 2023
2 parents a5bbc2b + 759bb81 commit 8bca5ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 6 additions & 0 deletions 前沿技术/95.精读《Function VS Class 组件》.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ function usePrevious(value) {
`useState` 函数的参数虽然是初始值,但由于整个函数都是 Render,因此每次初始化都会被调用,如果初始值计算非常消耗时间,建议使用函数传入,这样只会执行一次:

```jsx
// Bad
function FunctionComponent(props) {
const [rows, setRows] = useState(createRows(props.count));
}

// Good
function FunctionComponent(props) {
const [rows, setRows] = useState(() => createRows(props.count));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ Redux 数据流的 `connect` 装饰器就是全局访问点的一种设计。

```typescript
class Ball {
private _instance = undefined
private static _instance: Ball;

// 构造函数申明为 private,就可以阻止 new Ball() 行为
private constructor() {}

public static getInstance = () => {
if (this._instance === undefined) {
this._instance = new Ball()
public static getInstance() {
if (Ball._instance === undefined) {
Ball._instance = new Ball();
}

return this._instance
return Ball._instance;
}
}

Expand Down

0 comments on commit 8bca5ad

Please sign in to comment.