如何在 React/Jsx 中的渲染器中调用函数
How to Call a Function inside a Render in React/Jsx
我想在一些嵌入式 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>
);
}
}
要调用您必须添加的函数 ()
{this.renderIcon()}
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'));
<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>
这取决于您的需要,您可以使用 this.renderIcon()
或 bind this.renderIcon.bind(this)
更新
这就是您在渲染外部调用方法的方式。
buttonClick(){
console.log("came here")
}
render() {
return (
<div className="patient-container">
<button onClick={this.buttonClick.bind(this)}>Click me</button>
</div>
);
}
推荐的方式是单独写一个组件导入。
修复在已接受的答案中。然而,如果有人想知道它为什么起作用以及为什么 SO 问题中的实现不起作用,
首先,函数是 JavaScript 中的第一个 class 个对象。这意味着它们像任何其他变量一样被对待。函数可以作为参数传递给其他函数,可以由另一个函数返回,也可以作为值赋给变量。 Read more here.
所以我们使用该变量通过在末尾添加括号 ()来调用函数。
有一件事,
如果你有一个函数 returns 一个函数,你只需要调用那个返回的函数,你可以在调用外部函数 ()() 时使用双括号。
我想在一些嵌入式 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>
);
}
}
要调用您必须添加的函数 ()
{this.renderIcon()}
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'));
<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>
这取决于您的需要,您可以使用 this.renderIcon()
或 bind this.renderIcon.bind(this)
更新
这就是您在渲染外部调用方法的方式。
buttonClick(){
console.log("came here")
}
render() {
return (
<div className="patient-container">
<button onClick={this.buttonClick.bind(this)}>Click me</button>
</div>
);
}
推荐的方式是单独写一个组件导入。
修复在已接受的答案中。然而,如果有人想知道它为什么起作用以及为什么 SO 问题中的实现不起作用,
首先,函数是 JavaScript 中的第一个 class 个对象。这意味着它们像任何其他变量一样被对待。函数可以作为参数传递给其他函数,可以由另一个函数返回,也可以作为值赋给变量。 Read more here.
所以我们使用该变量通过在末尾添加括号 ()来调用函数。
有一件事, 如果你有一个函数 returns 一个函数,你只需要调用那个返回的函数,你可以在调用外部函数 ()() 时使用双括号。