简体   繁体   English

我的if语句在做什么?

[英]What am I doing wrong with my if statements?

I'm porting a website I had build already for another project into React and wanted to set a state for my navbar and was trying to set up my conditionals to render my pages first but it keeps throwing syntax error 我正在将已经为另一个项目建立的网站移植到React中,并想为我的导航栏设置状态,并试图设置条件以首先呈现我的页面,但它不断抛出语法错误

I tried changing the {} enclosing to if to () to see if that did anything. 我尝试将{}括起来的if更改为(),以查看是否有任何作用。 I also tried wrapping my if inside {()} and nothing 我也尝试过将if包裹在{()}里面,什么也没有

class App extends Component {
  state = {
    page: 0,
  }
  setPageChange = () => {
    setState({ page: props.key })
  }

  render() {
    return (
      <div>
        <NavBar />
        {if (this.state.page === 1) {
          return (<Products />)
        } else if(this.state.page === 2) {
          return (<Contact />)
        } else {
          <Landing />
        }}
        <Footer />
      </div>
    );
  }
}

Doesn't matter with the bracket, you can try this: 括号无关紧要,您可以尝试以下操作:

class App extends Component {
  state = {
    page: 0,
  }
  setPageChange = () => {
    setState({ page: props.key })
  }

  renderContent() {
    if (this.state.page === 1) {
      return <Products />;
    } else if(this.state.page === 2) {
      return <Contact />;
    } else {
      return <Landing />;
    }
  }

  render() {
    return (
      <div>
        <NavBar />
        {this.renderContent()}
        <Footer />
      </div>
    );
  }
}

您应该使用条件渲染,例如

{this.state.page === 1 ? <Products /> : this.state.page === 2 ? <Contact /> : <Landing />}

Another way to approach this would be: 解决此问题的另一种方法是:

  render() {
    const { page } = this.state;

    return (
      <div>
        <NavBar />
        { (page === 1) && <Products /> }
        { (page === 2) && <Contact /> }
        { (page !== 1 && page !== 2 ) && <Landing /> }
        <Footer />
      </div>
    );
  }

The easiest way for this is like this: 最简单的方法是这样的:

class App extends Component {
  state = {
    page: 0,
  }
  setPageChange = () => {
    setState({ page: props.key })
  }

  render() {
    const { page } = this.state;

    return (
      <div>
        <NavBar />
        {page === 1 && <Products />}
        {page === 2 && <Contact />}
        {page !== 1 && page !== 2 &&
          <Landing />
        }
        <Footer />
      </div>
    );
  }
}

One alternate way is to use a mapper object and default value 一种替代方法是使用映射器对象和默认值

class App extends Component {
  state = {
    page: 0,
  }
  setPageChange = () => {
    setState({ page: props.key })
  }

  renderContent(val) {
    let obj = {
      1: Products,
      2: Contact,
    }
    return obj[val] || Landing
  }

  render() {
    let Component = this.renderContent(this.state.page)
    return (
      <div>
        <NavBar />
          <Component />
        <Footer />
      </div>
    );
  }
}

Here is a quick demo 这是一个快速演示

 function NavBar() { return <h1>Hello Navbar</h1>; } function Footer() { return <h1>Hello, Footer</h1>; } function Landing() { return <h1>Hello, Landing</h1>; } function Contact() { return <h1>Hello, Contact</h1>; } function Products() { return <h1>Hello, Products</h1>; } class App extends React.Component { state = { page: 1, } setPageChange = () => { setState({ page: props.key }) } renderContent() { if (this.state.page === 1) { return <Products />; } else if(this.state.page === 2) { return <Contact />; } else { return <Landing />; } } render() { return ( <div> <NavBar /> {this.renderContent()} <Footer /> </div> ); } } ReactDOM.render(<App />, document.getElementById("root")); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div> 

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

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