简体   繁体   English

在 React js 上单击按钮时如何重定向到另一个页面?

[英]How to redirect to another page when the button is clicked on React js?

Im making a google map with a report button and I cant figure out how to make the page redirect to another page when the button with class "report-btn" is clicked?我正在制作带有报告按钮的谷歌地图,但我无法弄清楚当单击类为“report-btn”的按钮时如何使页面重定向到另一个页面? I tried to used but it didnt work and Im not sure how to go about implementing this.我尝试使用但它没有用,我不知道如何去实现这个。

export class MapContainer extends Component {
  state = {
    showingInfoWindow: false,
    activeMarker: {}, 
    selectedPlace: {}, 
  };

  onMarkerClick = (props, marker, e) =>
    this.setState({
      selectedPlace: props,
      activeMarker: marker,
      showingInfoWindow: true,
    });
  onClose = (props) => {
    if (this.state.showingInfoWindow) {
      this.setState({
        showingInfoWindow: false,
        activeMarker: null,
      });
    }
  };

  render() {
    return (
      <Map
        google={this.props.google}
        zoom={14}
        style={mapStyles}
        initialCenter={{
          lat: 40.75901,
          lng: -73.984474,
        }}
      >
        <Marker onClick={this.onMarkerClick} name={"S Train - 42 Street"} />
        <InfoWindow
          marker={this.state.activeMarker}
          visible={this.state.showingInfoWindow}
          onClose={this.onClose}
        >
          <div>
            <h4>{this.state.selectedPlace.name}</h4>
            <button className="report-btn">Report</button>
          </div>
        </InfoWindow>
      </Map>
    );
  }
}

您有2 solutions ,使用react-router-domwindow.location.href/replace

import { Link, NavLink } from 'react-router-dom';

you should use你应该使用

import { Link} from 'react-router-dom';


  return (
    <Link to={"/somewhereyoulike}>
        <Button>
          somthiong
        </Button>
    </Link>

)

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

相关问题 单击表单的搜索按钮时从API获取数据,并在React JS的另一页上显示数据 - Fetch data from API when form's search button clicked and show data on another page in React JS 如何在React中将按钮重定向到另一个页面 - How to make a button redirect to another page in React React Router:单击链接/按钮时如何重定向到不同的组件? - React Router: How to redirect to a different component when a link/button is clicked? 单击超链接时如何重定向到服务器上的另一个页面? - How to redirect to another page on the server when hyperlink is clicked? "在 React 中单击另一个按钮时如何隐藏按钮?" - How to hide button when another button is clicked in React? 单击按钮时如何在另一页上显示按钮名称 - How to display button name on another page when button is clicked 如何使用React Native JS将页面重定向到另一页面 - How to redirect one page to another page using React native JS (React) 单击提交按钮时如何获取另一个组件的 state? - (React) How to get the state of another component when a submit button is clicked? 在反应中单击提交按钮时如何在同一页面上显示状态 - How to display the state on the same page when clicked Submit button in react 在react js中单击以进行测验时如何禁用按钮 - how to disable button when clicked in react js for quiz
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM