javascript - 如何在 React 中使用类组件之外的函数
问题描述
我想了解反应中的功能组件,但没有得到真实的图片。在这里,我拿了一些代码并对其进行了修改以供自己理解。任何帮助。
反应:
class App extends React.Component {
state = { count: 0, add: 2 }
handleIncrement() {
this.setState({ count: this.state.count + 1 })
}
handleDecrement() {
this.setState({ count: this.state.count - 1 })
}
anyMore() {
this.setState({ add: this.state.add })
this.setState({ count: this.state.count })
any(add, count)
}
render() {
return (
<div>
<button onClick={this.handleIncrement}>Increment by 1</button>
<button onClick={this.handleDecrement}>Decrement by 1</button> <br />
<button onClick={this.anyMore}>Add 2</button>
<div>
<p> {this.state.count} </p> <p> {this.state.add} </p>
</div>
</div>
)
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
function any() {
return a * b;
}
解决方案
只是想了解并找出解决方案,我认为您可以这样做
class App extends Component {
render() {
return (
Any()
);
}
}
const Any = () => {
return (
<div>
return a * b
</div>
);
};
export default App;
推荐阅读
- cordova - 添加 FCM 插件后的 Google 服务版本冲突:离子 3
- java - 防止 Spring 的 RestTemplate 为 multipart/form-data 中的每个参数添加标头
- php - 模态将计数器重置为 1
- angularjs - 带有jquery数据表搜索按钮的angularjs不起作用
- grafana - 创建了一个卷:权限被拒绝写入 /var/foo/
- ios - 在 NSPredicate 中解析 UUID
- tomcat - Tomcat JDBC 连接数与 netstat ESTABLISHED 连接数
- vue.js - vue js在线支付集成
- php - 脚本将部分内容写入 csv 文件
- mongodb - 水平扩展数据库指南