Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据React.js中的输入链接到其他网页?_Javascript_Reactjs_Web_React Router - Fatal编程技术网

Javascript 如何根据React.js中的输入链接到其他网页?

Javascript 如何根据React.js中的输入链接到其他网页?,javascript,reactjs,web,react-router,Javascript,Reactjs,Web,React Router,我基本上有一个React应用程序,它可以选择你是男性还是女性 如果你是男性,它会链接到一个网页,如果是女性,它会链接到其他网页 但实际上我不能这样做 import React, { Component } from 'react'; import './App.css'; import {Router , Route , browserHistory , Link} from 'react-router' import Card from './Card.js' class App extend

我基本上有一个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;
import React,{Component}来自'React';
导入“/App.css”;
从“react Router”导入{路由器、路由、浏览器历史记录、链接}
从“./Card.js”导入卡片
类应用程序扩展组件{
构造函数(){
超级();
此.state={
选项:“无”,
男:错
}
}
设置性别=(事件)=>{
让性别=event.target.value;
如果(性别==‘男性’){
this.setState({options:'male'})
}else if(性别==‘女性’){
this.setState({options:'female'})
}
否则{
console.log('未选择')
}
}
选项选定=(事件)=>{
if(this.state.options==='male'){
log('是的,它是男性');
console.log(事件)
this.setState({male:true});
}else if(this.state.options=='female'){
log(“是的,它是女性”);
//不了解如何在此处链接到页面
}否则{
console.log(“未选择”);
}
}
render(){
返回(
this.options selected(event)}/>
this.setGender(事件)}>
男性
女性
);
}
}
导出默认应用程序;
我可以这样做吗browserHistory.push(“/path to link”);-扎伊德刚才在编辑


请不要否决投票,因为我可能会被阻止

请您缩进/格式化您的代码。我该怎么做?链接到网页是什么意思,您是否配置了路由器,并且需要导航到另一条路由器您可以使用空格或制表符。+Shubham您能详细说明一下吗?我对这个问题不熟悉,请回答it@Zaid-如果你想重定向到另一个不在你的服务器上的页面,这种方式似乎可以解决问题。你说的服务器是什么意思?在我看来,这是最好的方式(香草javascript)。如果你想在你自己的应用程序中“路由”到另一个页面,当然有(react router redirect),但你会重定向到另一个路由/组件。我想通过使用browserHistory.push(“/path to link”);是否正确??
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 ...
    }
}