Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This course is archived!
While the concepts of this course are still largely applicable, it's built using an older version of Symfony (4) and React (16).

Refs

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Right now, we're using the name attribute of each form field to get the underlying DOM element. We use that to fetch its value.

This is interesting: most of the time in React, you communicate down to your elements - and your child components - via props. You use props when you render the element objects, and React handles creating the real DOM elements from that. The DOM is not something we normally touch directly.

But occasionally, you will want to access the underlying DOM elements. For example, you might want to read a value from a form field, call focus() on an element, trigger media playback if you're rendering a video tag or integrate with a third-party JavaScript library that needs you to pass it a DOM element.

Creating the refs

Because these are totally valid use-cases, React gives us a great system to access any DOM element called refs. We need to access two elements: the select element and the input. Cool! In the constructor, create 2 new properties: this.quantityInput = React.createRef() and this.itemSelect = React.createRef().

... lines 1 - 4
constructor(props) {
... lines 6 - 7
this.quantityInput = React.createRef();
this.itemSelect = React.createRef();
... lines 10 - 11
}
... lines 13 - 70

This just, "initialized" these two properties. The real magic is next: on the select, replace the name attribute with ref={this.itemSelect}. Do the same thing on the input: move the props onto their own lines, then add ref={this.quantityInput}.

... lines 1 - 24
render() {
return (
... lines 27 - 32
<select id="rep_log_item"
ref={this.itemSelect}
... lines 35 - 51
<input type="number" id="rep_log_reps"
ref={this.quantityInput}
... lines 54 - 62
);
}
... lines 65 - 70

To really get what this does, you need to see it. Comment out the onNewItemSubmit() call for a minute: it's temporarily broken. Then, let's console.log(this.quantityInput) and also this.itemSelect.

... lines 1 - 13
handleFormSubmit(event) {
... lines 15 - 18
console.log(this.quantityInput);
console.log(this.itemSelect);
... line 21
//onNewItemSubmit('Big Fat Cat', event.target.elements.namedItem('reps').value);
}
... lines 24 - 70

Moment of truth! Move over, Encore already refreshed the page. Fill out the fields, hit enter... cool! Each "ref" is an object with one property called current that is set to the underlying DOM element! Yea, I know, the fact that it sets the DOM element to a current key is a little weird... but it's just how it works.

Using the Refs

Thanks to this, let's set the DOM element objects onto two new variables: const quantityInput = this.quantityInput.current and const itemSelect = this.itemSelect.current. Below, log the field values: quantityInput.value and, this is a bit harder, itemSelect.options[itemSelect.selectedIndex], then, .value.

... lines 1 - 13
handleFormSubmit(event) {
... lines 15 - 17
const quantityInput = this.quantityInput.current;
const itemSelect = this.itemSelect.current;
... lines 20 - 21
console.log(quantityInput.value);
console.log(itemSelect.options[itemSelect.selectedIndex].value);
... lines 24 - 25
}
... lines 27 - 73

This finds which option is selected, then returns its value attribute. Try it: refresh, select "Big Fat Cat", enter 50 and... boom! People, this is huge! We can finally pass real information to the callback. Uncomment onNewItemSubmit. Pass the options code, but, change to .text: this is the display value of the option. And, until we actually starting saving things via AJAX, that is what we'll pass to the callback. Next, use quantityInput.value.

... lines 1 - 13
handleFormSubmit(event) {
... lines 15 - 20
onNewItemSubmit(
itemSelect.options[itemSelect.selectedIndex].text,
quantityInput.value
);
}
... lines 26 - 72

Updating the repLogs State

Finally, go back to RepLogApp and find handleNewItemSubmit: this is the function we just called. I'm going to change the argument to itemLabel, then clear things out. Ok, our job here is simple: add the new rep log to the repLogs state. Read the existing state with const repLogs = this.state.repLogs. Then, create the newRep set to an object. This needs the same properties as the other rep logs. So, add id set to... hmm. We don't have an id yet! Set this to "TODO". Then, reps: reps, itemLabel: itemLabel and, the last field is totalWeightLifted. Hmm, this is another tricky one! Our React app doesn't know how "heavy" each item is... and so we don't know the totalWeightLifted! Later, we'll need to ask the server for this info. For now, just use a random number.

... lines 1 - 24
handleNewItemSubmit(itemLabel, reps) {
const repLogs = this.state.repLogs;
const newRep = {
id: 'TODO-id',
reps: reps,
itemLabel: itemLabel,
totalWeightLifted: Math.floor(Math.random() * 50)
};
... lines 33 - 34
}
... lines 36 - 51

And finally, let's update the state! repLogs.push(newRep) and this.setState() with repLogs set to repLogs.

... lines 1 - 24
handleNewItemSubmit(itemLabel, reps) {
... lines 26 - 32
repLogs.push(newRep);
this.setState({repLogs: repLogs});
}
... lines 36 - 51

Um... there is a teeny problem with how we're updating the state here. But, we'll talk about it next. For now, gleefully forget I said anything was wrong and refresh! Fill out the form and... boo! A familiar error:

Cannot read property state of undefined in RepLogApp line 26

I've been lazy. Each time we create a handler function in a class, we need to bind it to this! In the constructor, add this.handleNewItemSubmit = the same thing .bind(this).

... lines 1 - 5
constructor(props) {
... lines 7 - 18
this.handleNewItemSubmit = this.handleNewItemSubmit.bind(this);
}
... lines 21 - 52

Using uuids

Try it again! We got it! It updates the state and that causes React to re-render and add the row. But... if we try it a second time, it does update the state, but, ah! It yells at us:

Encountered two children with the same key

Ah! The id property is eventually used in RepLogList as the key prop. And with the hardcoded TODO, it's not unique. Time to fix that temporary hack.

But, hmmm. How can we get a unique id? There are always two options. First, you can make an AJAX request and wait for the server to send back the new id before updating the state. We'll do that later. Or, you can generate a uuid in JavaScript. Let's do that now. And later, when we start talking to the server via AJAX, we'll discuss how UUIDs can still be used, and are a great idea!

To generate a UUID, find your terminal and install a library:

yarn add uuid --dev

Tip

In the latest version of uuid, you should import the uuid package like this:

import { v4 as uuid } from 'uuid';

Wait for that to finish... then go to RepLogApp and import uuid from uuid/v4. There are a few versions of UUID that behave slightly differently. It turns out, we want v4.

Down in constructor(), use UUID's everywhere, even in our dummy data. Then, move to the handle function and use it there.

... lines 1 - 3
import uuid from 'uuid/v4';
... lines 5 - 6
constructor(props) {
... lines 8 - 9
this.state = {
... line 11
repLogs: [
{ id: uuid(), reps: 25, itemLabel: 'My Laptop', totalWeightLifted: 112.5 },
{ id: uuid(), reps: 10, itemLabel: 'Big Fat Cat', totalWeightLifted: 180 },
{ id: uuid(), reps: 4, itemLabel: 'Big Fat Cat', totalWeightLifted: 72 }
]
};
... lines 18 - 20
}
... lines 22 - 26
handleNewItemSubmit(itemLabel, reps) {
... line 28
const newRep = {
id: uuid(),
... lines 31 - 33
};
... lines 35 - 36
}
... lines 38 - 53

Let's see if this fixes things! Move over, make sure the page is refreshed and start adding data. Cool: we can add as many as we want.

Clearing the Form

Which... is actually kinda weird: when the form submits, we need the fields to reset. No problem: in RepLogCreator, in addition to reading the values off of the DOM elements, we can also set them. At the bottom, use quantityInput.value = '' and itemSelect.selectedIndex = 0.

... lines 1 - 13
handleFormSubmit(event) {
... lines 15 - 25
quantityInput.value = '';
itemSelect.selectedIndex = 0;
}
... lines 29 - 75

Try it! Refresh... fill in the form and... sweet! Whenever you need to work directly with DOM elements, refs are your friend.

Leave a comment!

6
Login or Register to join the conversation
Carlos G. Avatar
Carlos G. Avatar Carlos G. | posted 3 years ago

This didn't work for me:

import uuid from 'uuid/v4';
...
repLogs: [
{ id: uuid(), reps: 25, itemLabel: 'My Laptop', totalWeightLifted: 112.5 },
...
]
...
handleAddRepLog(itemLabel, reps) {
const newRep = {
id: uuid(),
....

--------------------------------------------------------------------------
Got browser error:
"
The above error occurred in the <replogapp> component:
in RepLogApp
in div

Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-b... to learn more about error boundaries.
...
too much recursion

-----------------------------------------------------------------------------

Fix: https://github.com/uuidjs/u...

1 Reply

Hey Carlos G.!

Ah, so it seems that the import syntax changes for newer versions of uuid - thanks for posting that. We'll check into adding a note!

Cheers!

1 Reply
CloudCreators Avatar
CloudCreators Avatar CloudCreators | posted 2 years ago | edited

Warning: Unexpected ref object provided for select. Use either a ref-setter function or React.createRef().

Everything was working the refs was extracting and passing it to the function but when I try to update the sate by making new array it gives error.

My Set state code. I commented out everything except console and it works but after I removed the comments it doesnt work. It means the set state is causing a problem and IDK why I checked everywhere.


handleNewItemSubmit(itemLabel,reps){
        console.log(itemLabel,reps);
        const repLogs = this.state.repLogs;
        const newRep = {
            id: 'TODO-id',
            reps: reps,
            itemLabel: itemLabel,
            totalWeightLifted: Math.floor(Math.random() * 50)
        };
        repLogs.push(newRep);
        this.setState({repLogs: repLogs});
    }
Reply

Hey CloudCreators

You're modifying the state directly by doing repLogs.push(newRep);, you need to create a new array instead
This chapter may help you out https://symfonycasts.com/screencast/reactjs/immutable-state

Cheers!

Reply
m3tal Avatar

The HD video doesn't work,

Reply

Hey m3tal

Could you give it another try? It's working for me, it may have been a temporary outage

Cheers!

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

While the concepts of this course are still largely applicable, it's built using an older version of Symfony (4) and React (16).

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.2.0",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "doctrine/doctrine-bundle": "^1.6", // 1.9.1
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.3
        "doctrine/doctrine-fixtures-bundle": "~3.0", // 3.0.2
        "doctrine/doctrine-migrations-bundle": "^1.2", // v1.3.1
        "doctrine/orm": "^2.5", // v2.7.2
        "friendsofsymfony/jsrouting-bundle": "^2.2", // 2.2.0
        "friendsofsymfony/user-bundle": "dev-master#4125505ba6eba82ddf944378a3d636081c06da0c", // dev-master
        "sensio/framework-extra-bundle": "^5.1", // v5.2.0
        "symfony/asset": "^4.0", // v4.1.4
        "symfony/console": "^4.0", // v4.1.4
        "symfony/flex": "^1.0", // v1.17.6
        "symfony/form": "^4.0", // v4.1.4
        "symfony/framework-bundle": "^4.0", // v4.1.4
        "symfony/lts": "^4@dev", // dev-master
        "symfony/monolog-bundle": "^3.1", // v3.3.0
        "symfony/polyfill-apcu": "^1.0", // v1.9.0
        "symfony/serializer-pack": "^1.0", // v1.0.1
        "symfony/swiftmailer-bundle": "^3.1", // v3.2.3
        "symfony/twig-bundle": "^4.0", // v4.1.4
        "symfony/validator": "^4.0", // v4.1.4
        "symfony/yaml": "^4.0", // v4.1.4
        "twig/twig": "2.10.*" // v2.10.0
    },
    "require-dev": {
        "symfony/debug-pack": "^1.0", // v1.0.6
        "symfony/dotenv": "^4.0", // v4.1.4
        "symfony/maker-bundle": "^1.5", // v1.5.0
        "symfony/phpunit-bridge": "^4.0", // v4.1.4
        "symfony/web-server-bundle": "^4.0" // v4.1.4
    }
}

What JavaScript libraries does this tutorial use?

// package.json
{
    "dependencies": {
        "@babel/plugin-proposal-object-rest-spread": "^7.12.1" // 7.12.1
    },
    "devDependencies": {
        "@babel/preset-react": "^7.0.0", // 7.12.5
        "@symfony/webpack-encore": "^0.26.0", // 0.26.0
        "babel-plugin-transform-object-rest-spread": "^6.26.0", // 6.26.0
        "babel-plugin-transform-react-remove-prop-types": "^0.4.13", // 0.4.13
        "bootstrap": "3", // 3.3.7
        "copy-webpack-plugin": "^4.4.1", // 4.5.1
        "core-js": "2", // 1.2.7
        "eslint": "^4.19.1", // 4.19.1
        "eslint-plugin-react": "^7.8.2", // 7.8.2
        "font-awesome": "4", // 4.7.0
        "jquery": "^3.3.1", // 3.3.1
        "promise-polyfill": "^8.0.0", // 8.0.0
        "prop-types": "^15.6.1", // 15.6.1
        "react": "^16.3.2", // 16.4.0
        "react-dom": "^16.3.2", // 16.4.0
        "sass": "^1.29.0", // 1.29.0
        "sass-loader": "^7.0.0", // 7.3.1
        "sweetalert2": "^7.11.0", // 7.22.0
        "uuid": "^3.2.1", // 3.4.0
        "webpack-notifier": "^1.5.1", // 1.6.0
        "whatwg-fetch": "^2.0.4" // 2.0.4
    }
}
userVoice