Javascript 在类中使用If-else。
我们可以在Javascript类中使用Javascript 在类中使用If-else。,javascript,reactjs,Javascript,Reactjs,我们可以在Javascript类中使用if-else语句吗 我在有状态的课堂上学习 在课堂上,我做了类似的事情 import React, { Component } from 'react'; import Aux from '../../HOC/Aux.js'; import Burger from '../../burger/burger.js' import BuildControls from '../../burger/Build-Control/build-controls.js'
if-else语句吗
我在有状态的课堂上学习
在课堂上,我做了类似的事情
import React, { Component } from 'react';
import Aux from '../../HOC/Aux.js';
import Burger from '../../burger/burger.js'
import BuildControls from '../../burger/Build-Control/build-controls.js';
class BurgerBuilder extends Component {
state = {
ingredient: {
salad: 0,
bacon: 0,
cheese: 0,
meat: 0
},
totalPrice: 4,
purchaseable: false
}
//Something
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
但后来它开始抛出这个错误
Unexpected token (63:4)
61 |
62 |
> 63 | if (this.state.totalPrice != 4) {
| ^
64 | let newState = {
65 | ...this.state.ingredient
66 | }
如果我们可以使用If-else语句,那么您知道这为什么不起作用吗?您可以将该逻辑移动到一个方法setIngredientState
,并从构造函数中调用它
代码:
您可以将该逻辑移动到方法setIngredientState
,并从构造函数中调用它
代码:
使用组件时,应在函数中使用if语句
您可以在安装组件时执行此操作,也可以在组件更新时执行此操作,如下所示:
*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
render() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
当组件呈现如下方式时,也可以设置if语句:
*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
render() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
另请注意,您不应该使用=代码>在Javascript中比较值时,最好使用==因为它还比较类型,而不仅仅是值。在您的情况下,“4”和“4”是相同的。在处理组件时,应该在函数中使用if语句
您可以在安装组件时执行此操作,也可以在组件更新时执行此操作,如下所示:
*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
render() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
当组件呈现如下方式时,也可以设置if语句:
*/ This will only occur once, at the moment the component is mounted */
componentDidMount() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
// This will happen every time a prop or state changes, which triggers a re-render of the component. This can trigger a recursive loop if you're not careful
componentDidUpdate() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
render() {
if (this.state.totalPrice != 4) {
let newState = {
...this.state.ingredient
}
this.setState({ingredient: newState})
}
}
另请注意,您不应该使用=代码>在Javascript中比较值时,最好使用==因为它还比较类型,而不仅仅是值。在您的情况下,“4”和“4”是相同的。if-else语句
应该位于函数中或构造函数内部
至少是这样。否。如果需要条件,请在函数中执行。If-else语句
应在函数中
或在构造函数中
至少。否。如果需要条件,请在函数中执行。