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

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

How do I add 2 links in condition? ReactJS

问题

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

  1. handleSubmit = () => {
  2. const { lang, email } = this.state;
  3. const { search } = this.props;
  4. if (isEmailValid(email)) {
  5. if (search) {
  6. window.location =
  7. lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + search + '&email=' + email;
  8. } else {
  9. window.location = lang === 'fr' ? 'https://example.com/inscription?email=' + email : 'https://example.com/signup?email=' + email;
  10. }
  11. } else {
  12. this.setState({
  13. error: lang.startBanner.error,
  14. });
  15. }
  16. }

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

  1. if (search) {
  2. window.location =
  3. this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + search + '&email=' + email;
  4. } else {
  5. window.location = this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + email;
  6. }
  1. state = {
  2. lang: 'fr',
  3. search: '',
  4. email: '',
  5. error: '',
  6. windowWidth: undefined,
  7. }
  8. componentDidMount() {
  9. if (typeof window !== 'undefined' && window) {
  10. this.handleResize();
  11. window.addEventListener('resize', this.handleResize);
  12. this.setState({
  13. lang: window.location.pathname.includes('/fr') ? 'fr' : 'en',
  14. search: window.location.search ? window.location.search : '',
  15. });
  16. }
  17. }
英文:

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.

  1. handleSubmit = () => {
  2. const { lang, email } = this.state
  3. const { search } = this.props
  4. const { lang } = this.state
  5. if (isEmailValid(email)) {
  6. if (search) {
  7. window.location =
  8. 'https://example.com/inscription' + search + '&email=' + email
  9. } else {
  10. window.location = 'https://example.com/inscription?email=' + email
  11. }
  12. } else {
  13. this.setState({
  14. error: lang.startBanner.error,
  15. })
  16. }

}

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

  1. if (search) {
  2. window.location =
  3. this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + search + '&email=' + email
  4. // 'https://example.com/inscription' + search + '&email=' + email
  5. } else {
  6. // window.location = 'https://example.com/inscription?email=' + email
  7. window.location = this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup' + email
  8. }
  1. lang: fr,
  2. search: '',
  3. email: '',
  4. error: '',
  5. windowWidth: undefined,
  6. }
  7. componentDidMount() {
  8. if (typeof window !== 'undefined' && window) {
  9. this.handleResize()
  10. window.addEventListener('resize', this.handleResize)
  11. this.setState({
  12. lang: window.location.pathname.includes('/fr') ? fr : en,
  13. search: window.location.search ? window.location.search : '',
  14. })
  15. }
  16. }```
  17. </details>
  18. # 答案1
  19. **得分**: 1
  20. 这应该可以工作
  21. ```javascript
  22. window.location = this.state.lang === 'fr' ? 'https://example.com/inscription' : 'https://example.com/signup';

HandleSubmit:

  1. handleSubmit() {
  2. const { lang, email, search } = this.state;
  3. const url = this.state.url + (this.state.lang === "fr" ? "inscription" : "signup");
  4. if (this.isEmailValid(email)) {
  5. if (search) {
  6. window.location = url + search + "&email=" + email;
  7. } else {
  8. window.location = `${url}?email=` + email;
  9. }
  10. } else {
  11. this.setState({
  12. error: lang.startBanner.error
  13. });
  14. }
  15. }

我还更新了示例

英文:

This should work

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

HandleSubmit :

  1. handleSubmit() {
  2. const { lang, email, search } = this.state;
  3. const url = this.state.url + (this.state.lang === &quot;fr&quot; ? &quot;inscription&quot; : &quot;signup&quot;)
  4. if (this.isEmailValid(email)) {
  5. if (search) {
  6. window.location = url + search + &quot;&amp;email=&quot; + email;
  7. } else {
  8. window.location = `${url}?email=` + email;
  9. }
  10. } else {
  11. this.setState({
  12. error: lang.startBanner.error
  13. });
  14. }
  15. }

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:

确定