简体   繁体   English

除非刷新页面,否则“重置”按钮将不起作用,否则会给我一个错误(React.JS)

[英]“Reset” button won't work unless it refreshes page, and gives me an error otherwise (React.JS)

So I'm working in React.JS, and I'm trying to make something like an input field, where a user can enter a number of their choice, and it will show up in the text.所以我在 React.JS 中工作,我正在尝试制作类似于输入字段的东西,用户可以在其中输入他们选择的数字,它会显示在文本中。

I decided to add a "Reset to zero" button, as an extension.我决定添加一个“重置为零”按钮作为扩展。

             <div>
                Count: {this.state.value}
                <form>
                <input type="number" value = {this.state.value} onChange = {this.inputChange}/>
                <br/>
                <button onClick = {this.reset}>reset to zero</button>
                </form>     
            </div>          

It works, but it refreshes the page every time it does so.它可以工作,但每次它都会刷新页面。

I read online, and I decided to add "type=button" to my button as so:我在线阅读,我决定将“type = button”添加到我的按钮中:

<button type="button" onClick = {this.reset}>reset to zero</button>

When I run my code again, it still increments fine, but when I click the button, nothing happens, and when I try to increment it again, I get an error, "TypeError: this.setState is not a function".当我再次运行我的代码时,它仍然会正常增加,但是当我单击按钮时,什么也没有发生,当我尝试再次增加它时,我收到一个错误,“TypeError: this.setState is not a function”。

The error is coming from this method:错误来自此方法:

inputChange = event => {
        this.setState ({value: event.target.value})
    }

I know where the error is coming from, but I don't know why it happened, or how to fix it (note that I'm also a beginner at JavaScript and React.JS)我知道错误来自哪里,但我不知道它为什么会发生,或者如何解决它(请注意,我也是 JavaScript 和 React.JS 的初学者)

I hope someone can help me.我希望有一个人可以帮助我。

Here's my code in entirety, for reference.这是我的完整代码,供参考。

class Slider extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            value: 0
        }
    }
    inputChange = event => {
        this.setState ({value: event.target.value})
    }
    reset = () => {
        this.setState = ({
            count: 0
        })
    }
    render() {
        return (
            <div>
                Count: {this.state.value}
                <form>
                <input type="number" value = {this.state.value} onChange = {this.inputChange}/>
                <br/>
                <button type = "button"onClick = {this.reset}>reset to zero</button>
                </form>     
            </div>

        );
    }
}

Thank you guys, in advance.谢谢各位,提前。

The reason nothing happens on reset and you are getting that error on input change, is that you are reassigning this.setState in your reset function rather than calling it.重置时没有任何反应并且您在输入更改时收到该错误的原因是您在重置this.setState中重新分配 this.setState 而不是调用它。 Also, you are setting count instead of value , which would lead to the wrong state being set.此外,您正在设置count而不是value ,这将导致设置错误的 state 。

This is what your reset function should be:这就是您的重置 function 应该是:

reset = () => {
    this.setState ({
        value: 0
    })
}

When you call this.setState , React will trigger a re-render in your component with the new state.当您调用this.setState时,React 将使用新的 state 在您的组件中触发重新渲染。

That is currently not happening when you click reset.当您单击重置时,目前没有发生这种情况。 On your subsequent call to inputChange, this.setState has been reassigned with an object, and is no longer callable, throwing that error.在您随后调用 inputChange 时, this.setState已被重新分配为 object,并且不再可调用,从而引发该错误。

Try replacing your button like this:尝试像这样替换您的按钮:

<button type = "button"onClick = {this.reset.bind(this)}>reset to zero</button>

This will force the method to execute being this the scope.这将强制this方法执行为 scope。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM