Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Displaying the Search Results

Tyler McDonald
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,676 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
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!