简体   繁体   English

如何在 Reactjs 中正确使用条件语句和内联 styles?

[英]How can I use conditional statements alongside inline styles correctly in Reactjs?

I am building a pizza builder react application.我正在构建一个披萨制造商反应应用程序。 An application where users can build pizzas in real-time and order them.一个应用程序,用户可以在其中实时构建比萨饼并订购它们。 I am trying to set some conditional styles based on the state and the input Handler but my styles are not being applied.我正在尝试基于 state 和输入处理程序设置一些有条件的 styles 但我的 styles 没有被应用。

This is my JSX code这是我的 JSX 代码

class PizzaBuilder extends Component {
  state = {
    sauce: {
      selected: 'Marinara'
    }
  }
  toggleSauceHandler = (type) => () => {
    this.setState({
      toggle: type
    });
  }
  render() {
    return (
      <div className={styles.Sauce}>
        <div>
          <img style={this.state.toggle==="Marinara" ? {display: 'block', position: 'absolute', zIndex: '100'}: null} src={Marinara} alt="marinara" />
        </div>
      </div>
      <div>
        <input type="radio" name="sauce" id="Marinara" value="Marinara" onChange={this.toggleSauceHandler( "Marinara")} checked={this.state.toggle==="Marinara" }/>
        <label for="Marinara">Marinara</label>
      </div>
    )
  }
}

The Sauce itself is set to display none as I do not need the images showing on the page.酱汁本身设置为不显示,因为我不需要页面上显示的图像。 I only want to show the one by one based on the input from the radio button.我只想根据单选按钮的输入一一显示。

Check below link, may be helpful for you.检查下面的链接,可能对你有帮助。

React - Change Inline CSS Conditionally Based on Component State React - 根据组件有条件地更改内联 CSS State

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM