Skip to content
Fix Code Error

getting text input from a form in react.js using Refs

May 2, 2021 by Code Error
Posted By: Anonymous

Fairly new to React.js and I am running into a problem using Refs to grab my form data. I want to be able to pass the data to multiple components that I will be creating. I am using a tutorial that I purchased and everything works fine but when I use that information in my own app and submit the form I am getting the error below. Cant figure out for the life of me what I could be missing. The code for my component is below and the error I am receiving is pasted below as well. Any help is super appreciated. Thanks!

ERROR:

VM7473:1 Fetch API cannot load webpack://%5Bname%5D_%5Bchunkhash%5D/./node_modules/react-dom/cjs/react-dom.development.js?. URL scheme must be "http" or "https" for CORS request.
(anonymous) @ VM7473:1
_callee$ @ index.js:2429
tryCatch @ index.js:2616
invoke @ index.js:2850
prototype.(anonymous function) @ index.js:2668
step @ index.js:2493
(anonymous) @ index.js:2493
(anonymous) @ index.js:2493
(anonymous) @ index.js:2452
_callee2$ @ index.js:2421
tryCatch @ index.js:2616
invoke @ index.js:2850
prototype.(anonymous function) @ index.js:2668
step @ index.js:2493
(anonymous) @ index.js:2493
(anonymous) @ index.js:2493
map @ index.js:2489
getStackFrames @ index.js:2270
crashWithFrames @ index.js:1927
(anonymous) @ index.js:1942
errorHandler @ index.js:1992
invokeGuardedCallbackDev @ react-dom.development.js:199
invokeGuardedCallback @ react-dom.development.js:256
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:270
executeDispatch @ react-dom.development.js:561
executeDispatchesInOrder @ react-dom.development.js:583
executeDispatchesAndRelease @ react-dom.development.js:680
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:688
forEachAccumulated @ react-dom.development.js:662
runEventsInBatch @ react-dom.development.js:816
runExtractedEventsInBatch @ react-dom.development.js:824
handleTopLevel @ react-dom.development.js:4820
batchedUpdates$1 @ react-dom.development.js:18932
batchedUpdates @ react-dom.development.js:2150
dispatchEvent @ react-dom.development.js:4899
interactiveUpdates$1 @ react-dom.development.js:18987
interactiveUpdates @ react-dom.development.js:2169
dispatchInteractiveEvent @ react-dom.development.js:4876
error (async)
registerUnhandledError @ index.js:2005
listenToRuntimeErrors @ index.js:1941
startReportingRuntimeErrors @ index.js:1786
connect @ hot-dev-client.js:84
_default @ webpack-hot-middleware-client.js:89
./node_modules/next/dist/client/next-dev.js @ next-dev.js:21
__webpack_require__ @ bootstrap:789
fn @ bootstrap:150
0 @ module.js:22
__webpack_require__ @ bootstrap:789
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.js:1
AddOfferForm.js:12 Uncaught TypeError: Cannot read property 'value' of undefined
    at AddOfferForm.js:12
    at HTMLUnknownElement.callCallback (react-dom.development.js:149)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
    at invokeGuardedCallback (react-dom.development.js:256)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:270)
    at executeDispatch (react-dom.development.js:561)
    at executeDispatchesInOrder (react-dom.development.js:583)
    at executeDispatchesAndRelease (react-dom.development.js:680)
    at executeDispatchesAndReleaseTopLevel (react-dom.development.js:688)
    at forEachAccumulated (react-dom.development.js:662)
    at runEventsInBatch (react-dom.development.js:816)
    at runExtractedEventsInBatch (react-dom.development.js:824)
    at handleTopLevel (react-dom.development.js:4820)
    at batchedUpdates$1 (react-dom.development.js:18932)
    at batchedUpdates (react-dom.development.js:2150)
    at dispatchEvent (react-dom.development.js:4899)
    at interactiveUpdates$1 (react-dom.development.js:18987)
    at interactiveUpdates (react-dom.development.js:2169)
    at dispatchInteractiveEvent (react-dom.development.js:4876)
(anonymous) @ AddOfferForm.js:12
callCallback @ react-dom.development.js:149
invokeGuardedCallbackDev @ react-dom.development.js:199
invokeGuardedCallback @ react-dom.development.js:256
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:270
executeDispatch @ react-dom.development.js:561
executeDispatchesInOrder @ react-dom.development.js:583
executeDispatchesAndRelease @ react-dom.development.js:680
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:688
forEachAccumulated @ react-dom.development.js:662
runEventsInBatch @ react-dom.development.js:816
runExtractedEventsInBatch @ react-dom.development.js:824
handleTopLevel @ react-dom.development.js:4820
batchedUpdates$1 @ react-dom.development.js:18932
batchedUpdates @ react-dom.development.js:2150
dispatchEvent @ react-dom.development.js:4899
interactiveUpdates$1 @ react-dom.development.js:18987
interactiveUpdates @ react-dom.development.js:2169
dispatchInteractiveEvent @ react-dom.development.js:4876
react-dom.development.js:289 Uncaught TypeError: Cannot read property 'value' of undefined
    at AddOfferForm.js:12
    at HTMLUnknownElement.callCallback (react-dom.development.js:149)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
    at invokeGuardedCallback (react-dom.development.js:256)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:270)
    at executeDispatch (react-dom.development.js:561)
    at executeDispatchesInOrder (react-dom.development.js:583)
    at executeDispatchesAndRelease (react-dom.development.js:680)
    at executeDispatchesAndReleaseTopLevel (react-dom.development.js:688)
    at forEachAccumulated (react-dom.development.js:662)
    at runEventsInBatch (react-dom.development.js:816)
    at runExtractedEventsInBatch (react-dom.development.js:824)
    at handleTopLevel (react-dom.development.js:4820)
    at batchedUpdates$1 (react-dom.development.js:18932)
    at batchedUpdates (react-dom.development.js:2150)
    at dispatchEvent (react-dom.development.js:4899)
    at interactiveUpdates$1 (react-dom.development.js:18987)
    at interactiveUpdates (react-dom.development.js:2169)
    at dispatchInteractiveEvent (react-dom.development.js:4876)
(anonymous) @ AddOfferForm.js:12
callCallback @ react-dom.development.js:149
invokeGuardedCallbackDev @ react-dom.development.js:199
invokeGuardedCallback @ react-dom.development.js:256
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:270
executeDispatch @ react-dom.development.js:561
executeDispatchesInOrder @ react-dom.development.js:583
executeDispatchesAndRelease @ react-dom.development.js:680
executeDispatchesAndReleaseTopLevel @ react-dom.development.js:688
forEachAccumulated @ react-dom.development.js:662
runEventsInBatch @ react-dom.development.js:816
runExtractedEventsInBatch @ react-dom.development.js:824
handleTopLevel @ react-dom.development.js:4820
batchedUpdates$1 @ react-dom.development.js:18932
batchedUpdates @ react-dom.development.js:2150
dispatchEvent @ react-dom.development.js:4899
interactiveUpdates$1 @ react-dom.development.js:18987
interactiveUpdates @ react-dom.development.js:2169
dispatchInteractiveEvent @ react-dom.development.js:4876

CODE:

import React from 'react';

class AddOfferForm extends React.Component {

    nameRef = React.createRef();


    addOffer = (event) =>{
        // 1. Stop form from submitting
        event.preventDefault();
        // 2. get the text from that input
        console.log(this.nameRef.value.value);

        // 3. Do Something with my data
    }



    render() {
        return (
            <>
            <form onSubmit={this.addOffer}>
                <input 
                    name="name"
                    ref={this.nameRef} 
                    type = "text" 
                    placeholder="Name"
                />
                <input 
                    name="exp-date"
                    ref={this.expRef} 
                    type = "text" 
                    placeholder="Experation Date"
                />
                <textarea 
                    name="desc"
                    ref={this.descRef} 
                    placeholder="Desciption"
                />
                <input 
                    name="exempt"
                    ref={this.exemptRef} 
                    type = "text" 
                    placeholder="Exempt Description"
                />

                <button type="submit">+ Add Offer</button>
            </form>
            </>
        );
    }
}

export default AddOfferForm;

Solution

change this console.log(this.nameRef.value.value); for console.log(this.nameRef.current.value);

The documentation always helps => https://reactjs.org/docs/refs-and-the-dom.html

Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Maven2: Missing artifact but jars are in place
  • Why the value of input file missing when I input the another…
  • Pass props in Link react-router
  • Next.js - Warning: Prop `dangerouslySetInnerHTML` did not…
  • react-table has a issue with rendering in next.js
  • Understanding PrimeFaces process/update and JSF f:ajax…
  • commandButton/commandLink/ajax action/listener method not…
  • Clear and reset form input fields
  • How do I include certain conditions in SQL Count

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Material ui breaks on refresh in Next js

Next Post:

Next.js custom javascript not running on it’s own page

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error