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

标签 javascript reactjs ecmascript-6

我在 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/

相关文章:

javascript - D3.js:力导向图,边长与边权成正比

javascript - Node Express JS - 处理对服务器的同时 POST 请求

javascript - 时刻js中的大写第一个字母

reactjs - 意外的 token ,axios try-catch 语句内应为 ")"

javascript - 节点环境的单元测试新 Blob enzyme

javascript - 如何使用 JQuery 覆盖 css

javascript - 使用 jQuery 的 NetSuite ReSTLet

reactjs - 无法设置状态 Firestore 数据

javascript - 从另一个返回对象中的函数的返回对象解构变量

javascript - 根据数组中的值过滤对象