在 React 的 componentWillMount 中声明常量?
Declaration of constant in componentWillMount in React?
就性能而言,第 1 种方法和第 2 种方法哪个更好?
const getCookieValue = readCookie('my_var')
应该在顶部声明,或者因为它的用法只是在一个条件下,所以最好将它放在 if
语句
中
方法一
componentWillMount() {
const {data1, data2} = this.props
if(data1) {
const getCookieValue = readCookie('my_var')
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
或
方法二
componentWillMount() {
const {data1, data2} = this.props
const getCookieValue = readCookie('my_var')
if(data1) {
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
性能明智 - 方法 1,你回答了你的问题 - 因为它的使用只是在这样的情况下,所以最好把它放在里面
就性能而言,第 1 种方法和第 2 种方法哪个更好?
const getCookieValue = readCookie('my_var')
应该在顶部声明,或者因为它的用法只是在一个条件下,所以最好将它放在 if
语句
方法一
componentWillMount() {
const {data1, data2} = this.props
if(data1) {
const getCookieValue = readCookie('my_var')
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
或
方法二
componentWillMount() {
const {data1, data2} = this.props
const getCookieValue = readCookie('my_var')
if(data1) {
if(getCookieValue === 'test_string') {
// Statements ....
}
}
}
性能明智 - 方法 1,你回答了你的问题 - 因为它的使用只是在这样的情况下,所以最好把它放在里面