大家好,所以我发现了有关React的有趣任务,我有点不明白如何解决
任务: 为什么此代码不起作用?解决这个问题。
代码:
[HttpGet]
public async Task<ActionResult> Get()
{
try
{
return Ok(await repository.GetAllAsync());
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError,ex);
}
}
[HttpPost]
public async Task<ActionResult<TEntity>> Post(TEntity entity)
{
try
{
var result = await repository.AddAsync(entity);
return CreatedAtAction(nameof(Get), result.Id, result);
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex);
}
}
class Test extends React.Component {
constructor(props) {
super(props)
this.state = {
count: 1
}
}
handler() {
this.setState({count: this.state.count++})
}
render() {
console.log('render')
return (
<div>
<button onClick={this.handler}>Add 1</button>
<p>{this.state.count}</p>
</div>
);
}
}
ReactDOM.render(
<Test />,
document.getElementById("test"));
答案 0 :(得分:3)
您发送了错误的功能参考。 应该是这样的。
<button onClick={this.handler.bind(this)}>Add 1</button>
或
<button onClick={() => this.handler()}>Add 1</button>