简体   繁体   English

如何使 onClick 卡在反应中移动到另一个页面

[英]How to make onClick card to move to another page in react

I'm new to react, this is code for the restaurant section, it creates and maps the restaurant cards, what I want is if the user click on one of the restaurant cards it goes to that specific restaurant page which will be on http://localhost:3001/place/ResturantName, so the user can view the restaurant with more details我是新来的反应,这是餐厅部分的代码,它创建并映射餐厅卡,我想要的是,如果用户单击其中一张餐厅卡,它会转到 http 上的特定餐厅页面: //localhost:3001/place/ResturantName,所以用户可以查看餐厅的更多细节

can you please help me?你能帮我么?

This is the code这是代码

import React, { Component } from 'react';
import axios from 'axios';
import App from "../../App";
import Cards from "../../Card";

function CreateCards(resturants) {

//Handel the Music, Wifi, Partition (to transfer it from bolean form into string)
    let ifMusic;
    let ifWifi;
    let ifPartition;

    //Type1 = Music, Type2 = Wifi, Type3= Partition

    if (resturants.Type1 == true){
        ifMusic = "Music";
    }else{
        ifMusic = "No Music";
    }

    if (resturants.Type2 == true){
        ifWifi = "Wifi";
    }else{
        ifWifi = "No Wifi";
    }

    if (resturants.Type3 == true){
        ifPartition = "Partition";
    }else{
        ifPartition = "No Partition";
    }
        
    return(
        <Cards 
            key={resturants._id}
            theCardId={resturants._id}
            placeName={resturants.Name}
            stars={resturants.Rating}
            PRating={resturants.PRating}
            music= {ifMusic}
            img={resturants.icon} // need uploads file
            status={Status(resturants.OpenTime, resturants.CloseTime)}
            descreption={resturants.Description}
            wifi={ifWifi}
            partition={ifPartition}
        />
    );
}

// Check if the place is open or closed depending on the work hours
function Status (Open, Close){
    const date = new Date();
    var hours = date.getHours();
    if ((Open <= hours) && (hours < Close)){
        return "Open";
    }else{
        return "Close";
    }
}



export default class Resturants extends Component {
//constructor elemnts in login
    constructor(props){
        super(props);

//intialy no data enterd // the types are the filters for each place such as music wifi etc
    this.state = {
            resturants: [],
            TypeOne: false, // handel the first type and checkbox on the page (ex Music)
            TypeTwo: false,
            TypeThree: false,
            Type4: false, // Handel the radiobox
            Type5: false,
            filteredRestraunts:[], // handel the filterd resturants cards in array
            noPlaceFound: false,
    }
    this.onChangeMusic = this.onChangeMusic.bind(this);
    this.onChangeWifi = this.onChangeWifi.bind(this);
    this.onChangePartition = this.onChangePartition.bind(this);
    this.onChangePriceRatinglow = this.onChangePriceRatinglow.bind(this);
    this.onChangePriceRatinghigh = this.onChangePriceRatinghigh.bind(this);
    this.OnClickViewPlace = this.OnClickViewPlace.bind(this);
}
componentDidMount(){
    //Get Resturants data, filteredRestraunts used for filtring and sorting the cards
    axios.get('http://localhost:3000/places/sections/Resturant&Coffes')
        .then(resp => {
            console.log(resp)
            this.setState({
                resturants: resp.data, 
                filteredRestraunts:resp.data
        })
        // console.log(this.state.resturants)
        // console.log(this.state.filteredRestraunts)
    })
}

//========================================================//
// Filters
onChangeMusic(e){
    this.setState({TypeOne: e.target.checked})
    // console.log(e.target.checked);
    // console.log(this.state.TypeOne);
    let copy;
    if(e.target.checked === true){
        copy =  this.state.filteredRestraunts.filter(Type => {return Type.Type1 === e.target.checked})
        this.setState({ filteredRestraunts: copy })
        if(copy.length === 0){
            this.setState({noPlaceFound: true})
        }
    }else{
        if(this.state.TypeTwo === true ||this.state.TypeThree === true ){
            copy =this.state.filteredRestraunts;
            this.setState({ filteredRestraunts: copy })
            this.setState({noPlaceFound: false})
        }else{
            copy =this.state.resturants;
            this.setState({ filteredRestraunts: copy })
            this.setState({noPlaceFound: false})
        }
    } 
}

onChangeWifi(e){
    this.setState({TypeTwo: e.target.checked})
    // console.log(e.target.checked);
    let copy;
    if(e.target.checked === true){
        copy =  this.state.filteredRestraunts.filter(Type => {return Type.Type2 === e.target.checked})
        this.setState({ filteredRestraunts: copy })
        if(copy.length === 0){
            this.setState({noPlaceFound: true})
        }
    }else{
        if(this.state.TypeOne === true ||this.state.TypeThree === true ){
            copy =this.state.filteredRestraunts;
            this.setState({ filteredRestraunts: copy })
            this.setState({noPlaceFound: false})
        }else{
            copy =this.state.resturants;
            this.setState({ filteredRestraunts: copy })
            this.setState({noPlaceFound: false})
        }
    } 
}

onChangePartition(e){
    this.setState({TypeThree: e.target.checked})
    // console.log(e.target.checked);
    let copy;
    if(e.target.checked === true){
        copy =  this.state.filteredRestraunts.filter(Type => {return Type.Type3 === e.target.checked})
        this.setState({ filteredRestraunts: copy })
        if(copy.length === 0){
            this.setState({noPlaceFound: true})
        }
    }else{
        if(this.state.TypeOne === true ||this.state.TypeTwo === true ){
            copy =this.state.filteredRestraunts;
            this.setState({ filteredRestraunts: copy })
            this.setState({noPlaceFound: false})
        }else{
            copy =this.state.resturants;
            this.setState({ filteredRestraunts: copy })
            this.setState({noPlaceFound: false})
        }
    } 
}

//========================================================//
// Sort By
onChangePriceRatinglow(e){
    this.setState({Type4: e.target.checked})
    let copy;
    if(e.target.checked === true){
        copy =  this.state.filteredRestraunts.sort((a,b) => { return a.PRating.length - b.PRating.length})
        this.setState({ filteredRestraunts: copy })
        document.getElementById('Type5').checked = false;
        if(copy.length === 0){
            this.setState({noPlaceFound: true})
        }
    }else{
        copy =this.state.resturants;
        this.setState({ filteredRestraunts: copy })
        this.setState({noPlaceFound: false})
    }
}

onChangePriceRatinghigh(e){
    this.setState({Type5: e.target.checked})
    console.log(e.target.checked);
    let copy;
    if(e.target.checked === true){
        copy =  this.state.filteredRestraunts.sort((a,b) => { return b.PRating.length - a.PRating.length})
        this.setState({ filteredRestraunts: copy })
        document.getElementById('Type4').checked = false;
        if(copy.length === 0){
            this.setState({noPlaceFound: true})
        }
    }else{
        copy =this.state.resturants;
        this.setState({ filteredRestraunts: copy })
        this.setState({noPlaceFound: false})
    }
}
//========================================================//
OnClickViewPlace(resturants){
    window.location = `/place/${resturants.Name}`;
    console.log("hi");
}
//=========================================================//
render(){
    
    return(
        <div className="flexthem">
            <div className="Filters">
                <h4 className="FilterTitle">Filters</h4>
                <label className="Label1">Music
                <input className="Checkbox1" type="checkbox"  id="TypeOne"  onChange={this.onChangeMusic}></input></label>
                <label className="Label1">Wifi
                <input className="Checkbox2" type="checkbox"  id="TypeTwo"  onChange={this.onChangeWifi}></input></label>
                <label className="Label1">Partiotion
                <input className="Checkbox3" type="checkbox"  id="TypeThree"  onChange={this.onChangePartition}></input></label>
                <label className="Label2">Sort by: Price</label>
                <label className="Label3" >Lowest to heighest
                <input type="radio" className="RadioBox" id="Type4" onClick={this.onChangePriceRatinglow}></input></label>
                <label className="Label3">heighest to Lowest
                <input type="radio" className="RadioBox" id="Type5" onClick={this.onChangePriceRatinghigh}></input></label>
            </div>
            <div className="general-card" onClick={this.OnClickViewPlace(this.state.resturants)}> 
                {this.state.filteredRestraunts.map(CreateCards)}
            </div>
            <h1 className="noPlaceFound" style={{display: this.state.noPlaceFound ? 'block' : 'none' }}> No place found</h1>
        </div>
    );
}
}

You have to use window.location.href = newUrl instead of window.location = newUrl in the onClick handler function. You have to use window.location.href = newUrl instead of window.location = newUrl in the onClick handler function.

暂无
暂无

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

相关问题 如何将我的反应卡组件链接添加到另一个页面? - How to add link my react card component to another page? 如何在React中将按钮重定向到另一个页面 - How to make a button redirect to another page in React 如何通过react-router移至另一个网页 - how to move to another web page by react-router React - 移动到另一个 html 页面 - 有条件的 if - React - move to another html page - with conditional if 如何使用 React Js 中的 onClick 事件将表格行数据作为道具传递到另一个页面 - How to pass table row data as props to another page using onClick event in React Js React - 在 Button 的 OnClick 满足某些条件后如何重定向到另一个页面? - React - How can I redirect to another page after some conditions are met on the OnClick of a Button? 如何使弹出式onClick能够在其中输入文本并将内容发送到另一个页面? - How to make a popup onClick with hability to enter text inside and send the content to another page? React onClick 方法将变量设置为 true 但仅在页面重新加载时设置一秒钟 - 如何使更改永久化? - React onClick method sets variable to true but only for a second when the page reloads - how to make changes permanent? (react,antd,card) 如何使用 antd 卡组件阻止 onclick function 操作按钮? - (react,antd,card) How can I block the onclick function from action buttons with antd card component? 如何用另一个OnClick替换一个React组件? - How to replace one React Component with another OnClick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM