如何使用 React 渲染组件 onClick?
How to render a component onClick with React?
我正在尝试渲染从父组件传递的特定位置的地图。我正在使用 google-maps-react,我不确定两件事:
如何在渲染中使用 onClick
调用函数。以及如何在我的 class 中编写呈现我想要的组件的函数。这是我目前所拥有的:
import React, { Component } from 'react';
import yelp from 'yelp-fusion';
import xhr from 'xhr';
import GoogleMapContainer from './Map';
class BusinessCard extends Component {
constructor () {
super()
this.renderMap = this.renderMap.bind(this);
}
renderMap(){
<GoogleMapContainer barLat={bar.coordinates.latitude} barLong={bar.coordinates.longitude} />
}
render() {
const newCard = this.props.newCard
const bar = this.props.selectedBar
// console.log("this are the coordinates", bar["coordinates"])
if(bar.coordinates){
return (
<div>
<p>{bar.coordinates.longitude}</p>
<p>{bar.name}</p>
<img src={bar.image_url} />
<button> X </button>
<button onClick={newCard}> Yes </button>
</div>
)
} else {
return(
<div>Loading...</div>
)
}
}
}
export default BusinessCard;
目前,编译存在问题,因为 bar
在呈现时未定义。任何 suggestions/advice?
首先,在 React 组件中,render()
method is your bridge between the virtual DOM (kept in memory by React) and the concrete DOM that is shown to the user. I'd read more about a React component's lifecycle - 理解这个就是理解 React。
此外,为了在页面上显示您的GoogleMapContainer
,您需要在React render()
方法中调用您的方法renderMap()
,将结果存储在一个变量中并return它。
要在 onClick
中调用多个函数是完全可能的,请将一个函数传递给处理程序并在那里调用您想要的函数数。
检查这个例子:
import React, { Component } from 'react';
import yelp from 'yelp-fusion';
import xhr from 'xhr';
import GoogleMapContainer from './Map';
class BusinessCard extends Component {
constructor (props) {
super(props)
// LOOK MORE WHAT 'this' means!! <- the key of javascript = execution context
this.renderMap = this.renderMap.bind(this);
this.handleClick = this.handleClick.bind(this);
}
renderMap(){
// careful!!! bar is undefined. Look more what 'this' means in javascript
const bar = this.props.selectedBar;
return (
<GoogleMapContainer barLat={bar.coordinates.latitude} barLong={bar.coordinates.longitude} />
);
}
handleClick() {
const newCard = this.props.newCard;
// call the newCard function prop (if only is a function!!!)
newCard();
// another method call
this.anotherMethod();
}
anotherMethod() {
console.log('heyo!');
}
render() {
const newCard = this.props.newCard
const bar = this.props.selectedBar
// console.log("this are the coordinates", bar["coordinates"])
if(bar.coordinates){
const renderMap = this.renderMap();
return (
<div>
<p>{bar.coordinates.longitude}</p>
<p>{bar.name}</p>
<img src={bar.image_url} />
<button> X </button>
<button onClick={this.handleClick}> Yes </button>
{ renderMap }
</div>
)
} else {
return(
<div>Loading...</div>
)
}
}
}
我正在尝试渲染从父组件传递的特定位置的地图。我正在使用 google-maps-react,我不确定两件事:
如何在渲染中使用 onClick
调用函数。以及如何在我的 class 中编写呈现我想要的组件的函数。这是我目前所拥有的:
import React, { Component } from 'react';
import yelp from 'yelp-fusion';
import xhr from 'xhr';
import GoogleMapContainer from './Map';
class BusinessCard extends Component {
constructor () {
super()
this.renderMap = this.renderMap.bind(this);
}
renderMap(){
<GoogleMapContainer barLat={bar.coordinates.latitude} barLong={bar.coordinates.longitude} />
}
render() {
const newCard = this.props.newCard
const bar = this.props.selectedBar
// console.log("this are the coordinates", bar["coordinates"])
if(bar.coordinates){
return (
<div>
<p>{bar.coordinates.longitude}</p>
<p>{bar.name}</p>
<img src={bar.image_url} />
<button> X </button>
<button onClick={newCard}> Yes </button>
</div>
)
} else {
return(
<div>Loading...</div>
)
}
}
}
export default BusinessCard;
目前,编译存在问题,因为 bar
在呈现时未定义。任何 suggestions/advice?
首先,在 React 组件中,render()
method is your bridge between the virtual DOM (kept in memory by React) and the concrete DOM that is shown to the user. I'd read more about a React component's lifecycle - 理解这个就是理解 React。
此外,为了在页面上显示您的GoogleMapContainer
,您需要在React render()
方法中调用您的方法renderMap()
,将结果存储在一个变量中并return它。
要在 onClick
中调用多个函数是完全可能的,请将一个函数传递给处理程序并在那里调用您想要的函数数。
检查这个例子:
import React, { Component } from 'react';
import yelp from 'yelp-fusion';
import xhr from 'xhr';
import GoogleMapContainer from './Map';
class BusinessCard extends Component {
constructor (props) {
super(props)
// LOOK MORE WHAT 'this' means!! <- the key of javascript = execution context
this.renderMap = this.renderMap.bind(this);
this.handleClick = this.handleClick.bind(this);
}
renderMap(){
// careful!!! bar is undefined. Look more what 'this' means in javascript
const bar = this.props.selectedBar;
return (
<GoogleMapContainer barLat={bar.coordinates.latitude} barLong={bar.coordinates.longitude} />
);
}
handleClick() {
const newCard = this.props.newCard;
// call the newCard function prop (if only is a function!!!)
newCard();
// another method call
this.anotherMethod();
}
anotherMethod() {
console.log('heyo!');
}
render() {
const newCard = this.props.newCard
const bar = this.props.selectedBar
// console.log("this are the coordinates", bar["coordinates"])
if(bar.coordinates){
const renderMap = this.renderMap();
return (
<div>
<p>{bar.coordinates.longitude}</p>
<p>{bar.name}</p>
<img src={bar.image_url} />
<button> X </button>
<button onClick={this.handleClick}> Yes </button>
{ renderMap }
</div>
)
} else {
return(
<div>Loading...</div>
)
}
}
}