reactjs - 发送函数 throw `spread operator` 不起作用,但添加了状态值
问题描述
我正在传递一个函数和一个状态值抛出spread
运算符。中的state
值更新html
。但是当点击button
函数时根本没有调用。
使用reactjs 中的运算符发送function
给孩子的正确方法是什么?spread
这是我的代码:
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
class Counter extends React.Component {
render() {
return (
<div>
<div>{this.props.display}</div>
<button onClick={this.props.fun}>+</button> //onclick not works!!
</div>
);
}
}
class CounterParent extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0 //added
};
}
increase = () => { //not called
this.setState({
count: this.state.count + 1
});
};
render() {
let object = {
display: this.state.count,
func: this.increase
};
return (
<div >
<Counter {...object} />
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<CounterParent />, rootElement);
解决方案
在Counter
组件中,您拼错了func
名称。
class Counter extends React.Component {
render() {
return (
<div>
<div>{this.props.display}</div>
<button onClick={this.props.func}>+</button>
</div>
);
}
}
推荐阅读
- wso2 - 如何在 wso2 esb 中将以下示例的 xml 转换为 json
- python - 当确认和密码不相等时,如何让控制台再次询问确认?(我知道的愚蠢问题)
- wcf - 使用 BizTalk WCF-WebHttp 发送端口传递 URL 参数时的编码问题
- google-cloud-platform - 无法从 DataFlow SQL 访问 BigQuery
- macos - 试图在 Mac OS Catalina 上安装 GNUPlot... ../term/lua.trm:107:10: 致命错误: 'lua.h' 文件未找到
- django - 如何在 DRF 中保存多个对象
- tensorflow - 验证准确度非常低但训练准确度很好
- docker - Flink 在 Openshift 4.4.9 上没有上去比如 Running-state
- sql-server - XML 和 SQL Server 中的 int 是什么?
- mysql - 试图从 MySql 结果字符串中删除“/”