简体   繁体   English

重定向到 index.html 文件中的反应 onclick

[英]Redirect to index.html file in react onclick

I want to redirect to index.html on clicking the div in react class component我想在单击反应 class 组件中的 div 时重定向到 index.html
i already having one on click function for passing value and also i tried all the ways我已经有一个点击 function 用于传递价值,我也尝试了所有方法

  1. history.push历史推送
  2. windows.location windows.location
  3. withRouter but nothing works withRouter 但没有任何效果

and because of class component history.push gives a error as it cannot be used in class component并且由于 class 组件 history.push 给出错误,因为它不能在 class 组件中使用

This is my js code这是我的js代码

 import React, { Component } from 'react'; import axios from 'axios'; class Tweet extends Component { constructor(props) { super(props); this.state = { value: null, }; this.handleClickHai = this.handleClickHai.bind(this); this.handleClickHello = this.handleClickHello.bind(this); } handleClickHai(){ this.setState(state => ({ value: 'Hai' })); axios.get('/setvalue?value=Hi').then( function (response){ console.log(response) }).catch(function (error) { console.log(error) }); } handleClickHello(){ this.setState(state => ({ value: 'Hello' })); axios.get('/setvalue?value=Hello').then( function (response){ console.log(response) }).catch(function (error) { console.log(error) }); } render() { return ( <div className="greet" > <div className="hello" onClick={this.handleClickHello} > <h4>Hello</h4> </div> <div className="hi" onClick={this.handleClickHai}> <h4>Hi</h4> </div> </div> ); } } export default Tweet;

when i click the hello div it will pass the value to variable and also i want it to redirect to html page anyone help me with the code当我单击 hello div 时,它会将值传递给变量,并且我希望它重定向到 html 页面任何人都可以帮助我使用代码

Combine #1 and #3, wrap the component in withRouter and then use this.props.history.push()结合#1和#3,将组件包裹在withRouter中,然后使用this.props.history.push()

import {withRouter} from "react-router-dom";
import React, { Component } from 'react';
import axios from 'axios';

class Tweet extends Component {
    constructor(props) {
        super(props);
        this.state = {
          value: null,
        };
      
        this.handleClickHai = this.handleClickHai.bind(this);
        this.handleClickHello = this.handleClickHello.bind(this);
    }
    handleClickHai(){
        this.setState(state => ({
            value: 'Hai'
        }));
       
        axios.get('/setvalue?value=Hi')
            .then( function (response){ 
                console.log(response)
            })
            .catch(function (error) {
                console.log(error) 
            });

    }
// Redirects To home
    redirectToHome(){
     this.props.history.push('/');}
}
    handleClickHello(){
        
        this.setState(state => ({
            value: 'Hello'
        }));
            
        axios.get('/setvalue?value=Hello')
            .then( function (response){ 
                console.log(response)
            })
            .catch(function (error) {
                console.log(error) 
            });

    }
  
  render() {
    return (
      <div className="greet" >
        
        <div className="hello" onClick={this.handleClickHello} >    
          <h4>Hello</h4>
        </div>

        <div className="hi" onClick={this.handleClickHai}>
            <h4>Hi</h4>
        </div>
    </div>

      
    );
  }
}

export default withRouter(Tweet);

this is a general example to redirect user.这是重定向用户的一般示例。

import { useHistory } from 'react-router-dom'
....
....
const history = useHistory();
....
....
history.push('/') //here you can redirect to where you want.

greetings,问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM