0

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;
1
  • why do you log two values? "value.value"? nameRef should be the reference to the native dom element, there is not value.value on that Commented Nov 26, 2018 at 17:57

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

That took care of it. Thanks so much for the help!
You're welcome, you can help me with my first answer vote xd
0

You're not too far off. You need to wrap the React.createRef() inside of a constructor and use this.nameRef.current to access it inside of render. Check out the docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.