英文:
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 === '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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论