简体   繁体   中英

How do I add 2 links in condition? ReactJS

I would like to add 2 links redirection for 2 different language pages: like when i complete the email and the input should redirect to /signup or /inscription.

handleSubmit = () => {
const { lang, email } = this.state
const { search } = this.props
const { lang } = this.state

if (isEmailValid(email)) {
  if (search) {
    window.location =
      'https://example.com/inscription' + search + '&email=' + email
  } else {
    window.location = 'https://example.com/inscription?email=' + email
  }
} else {
  this.setState({
    error: lang.startBanner.error,
  })
}

}

Actually it only redirect to /inscription on FR page and EN page. I tried this:

if (search) {
    window.location =
    this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + search + '&email=' + email
      // 'https://example.com/inscription' + search + '&email=' + email

  } else {
    // window.location = 'https://example.com/inscription?email=' + email
    window.location = this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + email
  }
    lang: fr,
    search: '',
    email: '',
    error: '',
    windowWidth: undefined,
  }

  componentDidMount() {
    if (typeof window !== 'undefined' && window) {
      this.handleResize()
      window.addEventListener('resize', this.handleResize)
      this.setState({
        lang: window.location.pathname.includes('/fr') ? fr : en,
        search: window.location.search ? window.location.search : '',
      })
    }
  }```

This should work

window.location = this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup'

HandleSubmit :

  handleSubmit() {
    const { lang, email, search } = this.state;
    const url = this.state.url + (this.state.lang === "fr" ? "inscription" : "signup")
    if (this.isEmailValid(email)) {
      if (search) {
        window.location = url + search + "&email=" + email;
      } else {
        window.location = `${url}?email=` + email;
      }
    } else {
      this.setState({
        error: lang.startBanner.error
      });
    }
  }

I've also updated the example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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