我基本上有一个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”); – Zaid刚编辑

请不要拒绝投票,因为我可能会被阻止

最佳答案

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 ...
    }
}

关于javascript - 如何基于React.js中的输入链接到其他网页?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48833267/

10-11 11:35