los*_*193 51 javascript render function jsx reactjs
我想在一些嵌入式html中调用一个函数.我尝试了以下但是没有调用该函数.这是在render方法中调用函数的错误方法吗?
import React, { Component, PropTypes } from 'react';
export default class PatientTable extends Component {
constructor(props) {
super(props);
this.state = {
checking:false
};
this.renderIcon = this.renderIcon.bind(this);
}
renderIcon(){
console.log("came here")
return(
<div>Function called</div>
)
}
render() {
return (
<div className="patient-container">
{this.renderIcon}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
dkn*_*ack 89
要调用您必须添加的功能 ()
{this.renderIcon()}
Run Code Online (Sandbox Code Playgroud)
Saa*_*ran 14
class App extends React.Component {
buttonClick(){
console.log("came here")
}
subComponent() {
return (<div>Hello World</div>);
}
render() {
return (
<div className="patient-container">
<button onClick={this.buttonClick.bind(this)}>Click me</button>
{this.subComponent()}
</div>
)
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="app"></div>
Run Code Online (Sandbox Code Playgroud)
这取决于你的需要,你可以使用 this.renderIcon()
或绑定 this.renderIcon.bind(this)
UPDATE
这是您在渲染之外调用方法的方法.
buttonClick(){
console.log("came here")
}
render() {
return (
<div className="patient-container">
<button onClick={this.buttonClick.bind(this)}>Click me</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
建议的方法是编写一个单独的组件并导入它.
归档时间: |
|
查看次数: |
133650 次 |
最近记录: |