gpt4 book ai didi

javascript - 如何修复 JSX props 不应该使用 .bind() 错误

转载 作者:行者123 更新时间:2023-11-29 20:51:22 25 4
gpt4 key购买 nike

我有一个简单的组件,它在按钮上显示数据 onClick 事件。这是我的组件:

import React, { Component } from 'react';
import './cardCheck.css';

class CardCheck extends Component {
constructor(props) {
super(props);
this.state = { showMessage: false };
}

_showMessage = bool => {
this.setState({
showMessage: bool
});
};

render() {
return (
<div>
<div className="newsletter-container">
<h1>Enter the ID of your card:</h1>
<div className="center">
<input type="number" />
<input type="submit" value="Check" onClick={this._showMessage.bind(null, true)} />
</div>
<div className="results" />
{this.state.showMessage && (
<div>
hello world!
<button onClick={this._showMessage.bind(null, false)}>hide</button>
</div>
)}
</div>
<h1>Offers:</h1>
</div>
);
}
}

export default CardCheck;

代码有效,但我的控制台出现此错误:

JSX props should not use .bind()

我读到它并将我的功能更改为这样的箭头:

import React, { Component } from 'react';
import './cardCheck.css';

class CardCheck extends Component {
constructor(props) {
super(props);
this.state = { showMessage: false };
}

_showMessage = bool => () => {
this.setState({
showMessage: bool
});
};

render() {
return (
<div>
<div className="newsletter-container">
<h1>Enter the ID of your card:</h1>
<div className="center">
<input type="number" />
<input type="submit" value="Check" onClick={this._showMessage()} />
</div>
<div className="results" />
{this.state.showMessage && (
<div>
hello world!
<button onClick={this._showMessage()}>hide</button>
</div>
)}
</div>
<h1>Offers:</h1>
</div>
);
}
}

export default CardCheck;

错误消失了,但我的代码现在不起作用。使用箭头函数执行此操作并使其仍然有效的正确方法是什么?

最佳答案

绑定(bind)或使用箭头函数 is not suggested因为这些函数将在每次渲染时重新创建。这就是您看到这些警告的原因。不要使用箭头函数进行绑定(bind)或调用,而是将其与引用一起使用并稍微更改您的函数。

_showMessage = () =>
this.setState( prevState => ( {
showMessage: !prevState.showMessage,
}) );

我们不使用 bool 值,而是使用其先前的值更改 showMessage 值。在这里,我们将 setState 与使用先前状态的函数一起使用,因为 setState 本身是异步的。

并且在您的元素中,您将使用此函数及其引用。

<input type="submit" value="Check" onClick={this._showMessage} />

工作示例。

class CardCheck extends React.Component {
constructor(props) {
super(props);
this.state = { showMessage: false };
}

_showMessage = () =>
this.setState( prevState => ( {
showMessage: !prevState.showMessage,
}) );

render() {
return (
<div>
<div className="newsletter-container">
<h1>Enter the ID of your card:</h1>
<div className="center">
<input type="number" />
<input type="submit" value="Check" onClick={this._showMessage} />
</div>
<div className="results" />
{this.state.showMessage && (
<div>
hello world!
<button onClick={this._showMessage}>hide</button>
</div>
)}
</div>
<h1>Offers:</h1>
</div>
);
}
}

ReactDOM.render(
<CardCheck />,
document.getElementById("root")
);
<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="root"></div>

关于javascript - 如何修复 JSX props 不应该使用 .bind() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51694407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com