我基本上有一个React App,它选择你是男性还是女性 如果你是男性它链接到一个网页如果是女性它链接到其他网页 但在React我无法做到这一点
import React, { Component } from 'react';
import './App.css';
import {Router , Route , browserHistory , Link} from 'react-router'
import Card from './Card.js'
class App extends Component {
constructor(){
super();
this.state = {
options : 'none',
male : false
}
}
setGender = (event)=>{
let gender = event.target.value;
if(gender === 'MALE'){
this.setState({options : 'male'})
}else if(gender === 'FEMALE'){
this.setState({options : 'female'})
}
else{
console.log('not selected')
}
}
optionsSelected = (event)=>{
if(this.state.options === 'male'){
console.log('yes it is male');
console.log(event)
this.setState({male : true});
}else if(this.state.options === 'female'){
console.log('yes it is female');
// Not understanding what to do here to link to a page
}else{
console.log('not selected');
}
}
render() {
return (
<div>
<input type="submit" onClick={event => this.optionsSelected(event)}/>
<div onChange={event => this.setGender(event)}>
<input type="radio" value="MALE" name="gender"/> Male
<input type="radio" value="FEMALE" name="gender"/> Female
</div>
</div>
);
}
}
export default App;
我可以执行这个browserHistory.push(“/ path-to-link”); - 扎伊德刚刚编辑
请不要因为我可能被封锁而进行投票
答案 0 :(得分:1)
optionsSelected = (event) => {
if (this.state.options !== 'none') {
// redirect to male or female website
window.location.href = this.state.options === 'male' ? 'http://male.xyz' : 'http://female.xyz';
} else {
// show error msg or do other stuff ...
}
}