diff --git a/source/_posts/react.md b/source/_posts/react.md index a555bd8..758ba66 100644 --- a/source/_posts/react.md +++ b/source/_posts/react.md @@ -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
Hello, world!
; + } +} + +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
Hello, world!
; +} + +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
Hello, world!
; + } +} + +export default MyComponent; +``` + +### Forwarding Refs +```javascript +Copy code +import React from "react"; + +const FancyButton = React.forwardRef((props, ref) => ( + +)); + +// Usage +const ref = React.createRef(); +Click me!; +``` + +### 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 ; +} + +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 ( +
+ + +
+ ); +} + +export default Form; +``` \ No newline at end of file