我无法调用.bind(this)我用来设置变量状态的任何函数。当我尝试更改我的调用方式时.bind,它会正常运行,但页面上的任何内容都不会改变或受到影响。
import React, { Component } from 'react'
//import { Link } from 'react-router-dom';
import './Farmer.css';
export class FarmerProfile extends Component {
constructor(props) {
super(props);
this.state = { isHome: true, isReservations: false, isUpdateFarm: false, isCurrentCrops: false};
}
showHomeBox(){
this.setState = { isHome: true, isReservations: false, isUpdateFarm: false, isCurrentCrops: false};
}
showReservationsBox (){
this.setState = { isHome: false, isReservations: true, isUpdateFarm: false, isCurrentCrops: false};
console.log("tried");
}
showUpdateFarmBox(){
this.setState = { isHome: false, isReservations: false, isUpdateFarm: true, isCurrentCrops: false};
}
showCurrentCropsBox(){
this.setState = { isHome: false, isReservations: false, isUpdateFarm: false, isCurrentCrops: true};
}
render() {
return (
<html lang="en">
<head>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css'/>
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet"></link>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"></link>
<title>Farmer Profile Page</title>
</head>
<body>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"></link>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<button type="button" class="btn btn-primary" onClick= {this.showReservationsBox.bind(this)}>Primary</button>
<div class="container">
<div class="row profile">
<div class="col-md-3 border">
<div class="profile-sidebar">
<div class="profile-userpic">
<img src="https://k48b9e9840-flywheel.netdna-ssl.com/wp-content/uploads/2020/04/COVID-19-Relief_Small-Farms--1024x614.jpg" class="img-responsive" alt=""></img>
</div>
<div class="profile-usertitle">
<div class="profile-usertitle-farmname">
Farm Name
</div>
<div class="profile-usertitle-farmemail">
farmEmail@gmail.com
</div>
</div>
<div class="profile-userbuttons">
<button type="button" class="btn btn-success btn-sm">Follow</button>
<button type="button" class="btn btn-danger btn-sm">Message</button>
</div>
<div class="profile-usermenu">
<ul class="nav">
<li class="active">
<a href="#home" onClick={this.showHomeBox.bind(this)}>
<i class="glyphicon glyphicon-home"></i>
Home </a>
</li>
<li>
<a href="#reservations" onClick={this.showReservationsBox.bind(this)}>
<i class="glyphicon glyphicon-calendar"></i>
Reservations </a>
</li>
<li>
<a href="#update" onClick={this.showUpdateFarmBox().bind(this), console.log("CLICK")}>
<i class="glyphicon glyphicon-upload"></i>
Update Farm </a>
</li>
<li>
<a href="#crops" onClick={this.showCurrentCropsBox.bind(this)}>
<i class="glyphicon glyphicon-grain"></i>
Current Crops </a>
</li>
</ul>
</div>
</div>
</div>
<div class="col-md-9">
<div class="active-selection-content">
{this.state.isHome && <HomeBox />}
{this.state.isReservations && <ReservationsBox />}
{this.state.isCurrentCrops && <CurrentCropsBox />}
{this.state.isUpdateFarm && <UpdateFarmBox />}
</div>
</div>
</div>
</div>
</body>
</html>
)
}
}
class HomeBox extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
render(){
return(
<div>Home Box</div>
);
}
}
class ReservationsBox extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
render(){
return(
<div>Reservations Box</div>
);
}
}
class UpdateFarmBox extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
render(){
return(
<div>Update Farm Box</div>
);
}
}
class CurrentCropsBox extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
render(){
return(
<div>Current Crops Box</div>
);
}
}
export default FarmerProfile
我尝试改变定义函数的方式,调用函数的方式,检查应该调用哪个页面的方式。我不知道为什么它不允许我将它绑定到状态