如何在条件中添加2个链接?ReactJS

huangapple go评论80阅读模式
英文:

How do I add 2 links in condition? ReactJS

问题

我想要为两个不同语言页面添加2个链接重定向:
比如当我完成输入电子邮件后,应该重定向到/signup或/inscription。

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

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

实际上,它只在FR页面和EN页面上重定向到/inscription。
我尝试了这个:

if (search) {
  window.location =
    this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + search + '&email=' + email;
} else {
  window.location = this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + email;
}
state = {
  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 : '',
    });
  }
}
英文:

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 : '',
      })
    }
  }```


</details>


# 答案1
**得分**: 1

这应该可以工作

```javascript
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
    });
  }
}

我还更新了示例

英文:

This should work

window.location = this.state.lang === &#39;fr&#39; ? &#39;https://example.com/inscription&#39; : &#39;https://example.com/signup&#39;

HandleSubmit :

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

I've also updated the example

huangapple
  • 本文由 发表于 2020年1月3日 20:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定