我按照 Yarn 网站 ( https://yarnpkg.com/getting-started/install ) 上的 Yarn 安装说明进行操作。这些说明表明,在 Node 版本 16.10.0 及更高版本上,您可以通过使用以下命令启用 corepack 来安装 Yarn:
corepack enable
Run Code Online (Sandbox Code Playgroud)
但是,运行该命令会返回:
zsh: command not found: corepack
Run Code Online (Sandbox Code Playgroud)
我通过 Homebrew 安装了 Node 并node -v返回v17.0.1. 我使用的是运行 Big Sur 的 M1 Mac。
我正在阅读React文档并阅读这一行(加粗他们的):
所有React组件必须在其道具方面表现得像纯函数一样.
这是严格的要求还是设计原则,以避免意外行为?
Facebook提供的"不纯"功能的示例是修改其输入的功能.但看起来React组件可能会修改它作为输入接收的道具(我已经在下面的例子中反映了他们自己的例子).
JSX代码:
var Withdraw = React.createClass({
render() {
this.props.account.balance -= this.props.amount;
return (
<div>Balance: ${this.props.account.balance} </div>
)
}
})
var CounterContainer = React.createClass({
render() {
var account = { balance: 10 }
return (
<div>
<Withdraw account={account} amount={2} />
<Withdraw account={account} amount={2} />
</div>
)
}
})
React.render(<CounterContainer />, document.getElementById('app'))
Run Code Online (Sandbox Code Playgroud)
另外:我知道上面的内容可以写成无状态的功能组件,并且该状态不应该存储在外部this.state.
是否保证componentDidMount生命周期方法始终会在 之前执行(并完成)componentWillUnmount?
例如,在我的componentDidMount方法中,我正在注册订阅:
componentDidMount() {
this.unsubscribe = subscribe();
}
componentWillUnmount() {
this.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud)
如果componentDidMount不能保证componentWillUnmount在此之前执行并完成,则this.unsubscribe可能undefined会尝试调用它将引发运行时错误。