Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Displaying the Search Results

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 Points

Why do we need to use refs in this example, since React documentation seems to discourage them?

After reading up on refs from the React docs, it seems they discourage the use of them if the same functionality could be achieved declaratively (which I assume means without using refs).

In that case, why would Guil convert it to using refs?

1 Answer

Laura Coronel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Laura Coronel
Treehouse Teacher

Hey Tyler McDonald, At the beginning of the video Guil explains this! He says:

it's acceptable to use the searchText state to update the gif state here onSubmit especially in a small app like this one.

He's just showing you that you can use refs instead of creating a state. One reasoning he give is:

If you're building a larger app where searchText may be used in other parts of the app or you simply don't want a state that's dependent on another state. You can also use a ref to access the value of the input field.

You are right! React does prefer the first method, Guil is just showing a different way to do it. Hope this helps!