我正在尝试通过自定义组件关闭和打开react-google-maps中的信息窗口。正在调用切换方法,因为我检查了它是否已记录。这是代码:
/**
* Created by.
*/
import * as React from 'react'
import {Col, Row, Card, CardHeader, CardBody, CardColumns, CardText, CardFooter} from 'reactstrap'
import {InfoWindow, Marker} from 'react-google-maps'
export default class home extends React.Component {
state = {
isOpen: false
}
toggleOpen = () => {
this.setState(({ isOpen }) => (
{
isOpen: !isOpen,
}
));
if(this.state.isOpen)
console.log("state is open")
else
console.log("state is not open")
}
render()
{
const { isOpen } = this.state;
return (
<Marker
position={this.props.position}
onClick={this.toggleOpen}>
<InfoWindow isOpen={isOpen}>
<Card className="hovercard">
<Row>
<CardColumns sm={6} lg={3}>a
<CardHeader>
{this.props.homestay}
</CardHeader>
<CardText className="avatar">
<img alt="profile img" src={this.props.profilePic}/>
</CardText>
<div className="info">
<CardText>{this.props.descrip}</CardText>
</div>
<CardFooter>
{this.props.price}
</CardFooter>
</CardColumns>
</Row>
</Card>
</InfoWindow>
</Marker>
)
}
}
您可以在此处查看自己的代码: https://codesandbox.io/s/93258nn8m4 我无法提出信息 - 任何想法?
答案 0 :(得分:0)
为了控制InfoWindow
可见度,您必须渲染或不渲染<InfoWindow />
,而不是将isOpen
作为道具传递。
您应该如何做到这一点(according to the official docs):
{ isOpen && <InfoWindow>
<Card className="hovercard">
<Row>
<CardColumns sm={6} lg={3}>
a
<CardHeader>{this.props.homestay}</CardHeader>
<CardText className="avatar">
<img alt="profile img" src={this.props.profilePic} />
</CardText>
<div className="info">
<CardText>{this.props.descrip}</CardText>
</div>
<CardFooter>{this.props.price}</CardFooter>
</CardColumns>
</Row>
</Card>
</InfoWindow> }
答案 1 :(得分:0)
请注意,调用setState
并不会立即更改状态here,因此,console.log
之后的状态可能不准确。如果您需要一个有效的示例,check this out