Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用React清除输入字段_Javascript_Html_Dom_Reactjs - Fatal编程技术网

Javascript 使用React清除输入字段

Javascript 使用React清除输入字段,javascript,html,dom,reactjs,Javascript,Html,Dom,Reactjs,我不明白为什么不能使用以下简单模式清除字段值: onComponentDidMount: function () { this.clearFields(); }, clearFields: function () { document.getElementById('username_field').value = ''; document.getElementById('password_field').value = ''; }, 我不认为这是React的问题,我

我不明白为什么不能使用以下简单模式清除
字段值:

onComponentDidMount: function () {
    this.clearFields();
},

clearFields: function () {
    document.getElementById('username_field').value = '';
    document.getElementById('password_field').value = '';
},

我不认为这是React的问题,我认为还有其他问题,但我不确定发生了什么。但这片土地肯定没有被清理干净。稍后,我可以调用此.clearFields(),该函数确实可以按预期工作,但在组件首次装载时不能工作。

正确的React生命周期函数称为
componentDidMount
,而不是
onComponentDidMount


但是,如果也使用React呈现输入,则不希望这样做。更改状态中存储的值,并让呈现函数处理输入字段的值的设置,通常对应用程序更好、更相关。

同意您应该在getInitialState和这个.setState({})中执行此操作;当涉及到反应时