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

Simon Coates
Simon Coates
8,481 Points

How does ref open access to other parts of the app?

The tutor says something like "if you're building a larger app, where a searchText may be used in other parts of the app. Or you simply don't want a state that dependent on another state, you can also use a ref to access the value of the input field.".

I'm very uncertain how switching to a ref gives you broader access. The obvious way to make the searchText available to "other parts of the app" would be to lift the state. Does anyone have some insight as I'm feeling like I'm missing something. Appreciate it.

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

It seems there might be some confusion about the context, and understandably so, the line kind of threw me off for a moment too. But, a ref doesn't provide broader access across the app. Instead, in scenarios where searchText is not part of the "app state" and is only needed within the component (e.g., for direct access to the input value on form submission), a ref is a lightweight alternative to managing state.

For making searchText available across different parts of a larger app, lifting state would indeed be the standard approach. The key takeaway is: use state when you need React to track and react to changes; use ref for simple, non-reactive access to DOM elements or values.

Simon Coates
Simon Coates
8,481 Points

Thanks for the response. I feel a little less stupid.