Add section on Refs to React cheatsheet

This commit is contained in:
P.Akash committed 2024-07-24 13:54:16 +05:30
1 parent 0be2db696b
commit 749a1e4488
1 file changed
+131
+131
View File
@@ -537,3 +537,134 @@ export default useLocalStorage;
Note: Custom Hooks are reusable functions in React that contain logic shared across multiple components. They allow you
to extract stateful logic from components into standalone functions.
### Creating Refs in Class Components
```javascript
import React, { Component } from "react";
class MyComponent extends Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
componentDidMount() {
console.log(this.myRef.current); // Access the DOM element
}
render() {
return <div ref={this.myRef}>Hello, world!</div>;
}
}
export default MyComponent;
```
### Using Refs in Functional Components
```javascript
import React, { useRef, useEffect } from "react";
function MyComponent() {
const myRef = useRef(null);
useEffect(() => {
console.log(myRef.current); // Access the DOM element
}, []);
return <div ref={myRef}>Hello, world!</div>;
}
export default MyComponent;
```
### Callback Refs
```javascript
import React, { Component } from "react";
class MyComponent extends Component {
constructor(props) {
super(props);
this.myRef = null;
this.setRef = element => {
this.myRef = element;
};
}
componentDidMount() {
console.log(this.myRef); // Access the DOM element
}
render() {
return <div ref={this.setRef}>Hello, world!</div>;
}
}
export default MyComponent;
```
### Forwarding Refs
```javascript
Copy code
import React from "react";
const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));
// Usage
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
```
### Accessing DOM Elements with Refs
### Refs are often used to access and interact with DOM elements directly. Here's an example where we focus an input element using a ref:
```javascript
import React, { useRef, useEffect } from "react";
function FocusInput() {
const inputRef = useRef(null);
useEffect(() => {
// Focus the input element when the component mounts
inputRef.current.focus();
}, []);
return <input ref={inputRef} type="text" />;
}
export default FocusInput;
```
### Managing Focus with Refs
### You can also manage focus between multiple elements using refs:
```javascript
import React, { useRef } from "react";
function Form() {
const firstInputRef = useRef(null);
const secondInputRef = useRef(null);
const handleKeyDown = (e) => {
if (e.key === "Enter") {
secondInputRef.current.focus();
}
};
return (
<div>
<input ref={firstInputRef} type="text" onKeyDown={handleKeyDown} />
<input ref={secondInputRef} type="text" />
</div>
);
}
export default Form;
```