Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React : TypeError: Cannot read property 'value' of undefined

I am getting value from form in ReactJS but in meantime when I click on submit button it give me error TypeError: Cannot read property 'value' of undefined . Could someone Please check what problem I may face .

Thanks

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;

1 Answers

If you're using refs it should be this:

const storeName = this.myInput.current.value;
like image 136
rrd Avatar answered Apr 29 '26 11:04

rrd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!