gpt4 book ai didi

javascript - react : TypeError: Cannot read property 'value' of undefined

转载 作者:行者123 更新时间:2023-11-29 20:43:56 24 4
gpt4 key购买 nike

我在 ReactJS 中从表单中获取值,但与此同时,当我单击提交按钮时,它给我错误 TypeError: Cannot read property 'value' of undefined 。有人可以检查我可能面临的问题。

谢谢

import React, { Component } from 'react'
import { getFunName } from './helpers';

class StorePicker extends Component {
myInput=React.createRef();

goToStore=event=> {
// 1- Stopping form from submitting
event.preventDefault();

// 2 - Getting value from Input
const storeName = this.myInput.value.value ;

// 3 - Change the page to /store/whatever-you-entered
this.props.history.push(`/store/${storeName}`)
}


render() {
return (
<form className="store-selector" onSubmit={this.goToStore}>
<h2>Please Enter Enter a Store</h2>
<input type="text" required placeholder="Store Name" ref={this.myInput} defaultValue={getFunName()}/>
<button type="submit">Visit Store</button>
</form>
)
}
}

export default StorePicker;

最佳答案

如果你使用 refs,它应该是这样的:

const storeName = this.myInput.current.value;

关于javascript - react : TypeError: Cannot read property 'value' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925670/

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