In this tutorial, we'll delve into React Fragments and Refs. React Fragments help in grouping a list of children without adding extra nodes to the DOM, which can help optimize your applications. Refs, on the other hand, allow us to access DOM nodes directly within React. This can be useful when you need to change an element's value or trigger an imperative action.
What you'll learn:
- How to use React Fragments
- How to access and manipulate DOM nodes with Refs.
Prerequisites:
- Basic understanding of JavaScript and React.
Fragments are used when a component needs to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM.
Example:
import React, { Fragment } from 'react';
function Example() {
return (
<Fragment>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</Fragment>
);
}
export default Example;
In this example, two paragraphs are returned without a parent container. This reduces unnecessary DOM nodes.
Refs in React allows us to access and interact with DOM nodes or React elements created in the render method.
Example:
import React, { useRef } from 'react';
function TextInput() {
const inputRef = useRef(null);
const handleClick = () => {
inputRef.current.focus();
};
return (
<div>
<input ref={inputRef} type="text" />
<button onClick={handleClick}>Focus the input</button>
</div>
);
}
export default TextInput;
In this example, the inputRef
is attached to the input
element. When the button is clicked, the input receives focus.
import React, { Fragment } from 'react';
function FragmentExample() {
return (
<Fragment>
<h1>Title</h1>
<p>Description</p>
</Fragment>
);
}
export default FragmentExample;
In this example, the Fragment
wraps around the h1
and p
elements, allowing them to be returned as a group without adding an unnecessary DOM node.
import React, { useRef } from 'react';
function RefExample() {
const myRef = useRef(null);
const handleClick = () => {
myRef.current.value = 'Hello, World!';
};
return (
<div>
<input ref={myRef} type="text" />
<button onClick={handleClick}>Set Input Value</button>
</div>
);
}
export default RefExample;
In this example, the myRef
is attached to the input
element. When the button is clicked, the value of the input changes to 'Hello, World!'.
In this tutorial, we've learned how to use React Fragments to group a list of children without adding extra nodes to the DOM. We've also learned how to use Refs to access and manipulate DOM nodes directly within React.
Next Steps:
- Practice using fragments and refs in your own projects.
- Try to build a small application which uses both concepts.
Additional Resources:
- React Documentation
- React Fragments
- React Refs and the DOM
Exercise 1: Create a component that uses a fragment to return an unordered list with five list items.
Exercise 2: Create a component with a text input and a button. When the button is clicked, the text input should be cleared.
Exercise 3: Combine the first two exercises. Create a component with a text input, a button, and a list. When the button is clicked, the value of the text input should be added as a new list item. The text input should then be cleared. Use refs and fragments as needed.