-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDomElementsExample.jsx
115 lines (95 loc) · 4.21 KB
/
DomElementsExample.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { useRef, useEffect } from "react";
export const DomElementsExample = () => {
const content = {
heading:
"These examples showcase various ways useRef can be used to interact with and manipulate DOM elements directly within functional components in React. ",
};
// Example 1: Basic useRef to manipulate DOM directly
// Creating a ref to hold the reference to the div element
const basicRef = useRef();
// ------
// ------
// ------
// Example 2: Controlling input focus
// Creating a ref to hold the reference to the input element
const focusRef = useRef();
// ------
// ------
// ------
// Example 3: Changing innerHTML of an element
// Creating a ref to hold the reference to the div element
const htmlRef = useRef();
// ------
// ------
// ------
// Example 4: Changing the src of an image dynamically
// Creating a ref to hold the reference to the img element
const imageRef = useRef();
// ------
// ------
// ------
// Example 5: Smooth Scrolling to a Section
// Creating a ref to hold the reference to the section element
const sectionRef = useRef();
// Function to handle video play
// Function to handle smooth scrolling
const handleScrollToSection = () => {
// Scrolling to the section when the button is clicked
sectionRef.current.scrollIntoView({ behavior: "smooth" });
};
// Using useEffect to perform side effects after the component has mounted
useEffect(() => {
// Example 1: Changing the background color of the div to lightblue
basicRef.current.style.backgroundColor = "lightblue";
// Example 2: Setting focus on the input field
focusRef.current.focus();
// Example 3: Changing the innerHTML of the div to bold text
htmlRef.current.innerHTML = "<strong>I love React JS</strong>";
// Example 4: Changing the src attribute of the image to a placeholder image
imageRef.current.src =
"https://www.pixartprinting.es/blog/wp-content/uploads/2021/08/Cover_IKEA.jpg";
// Example 5: Playing the video
// videoRef.current.play();
}, []); // Empty dependency array means this useEffect runs once after initial render
// Rendering the component
return (
<div>
<h5>{content.heading}</h5>
{/* Example 1: A div that will have its background color changed */}
<div ref={basicRef} style={{ padding: "20px" }}>
Example 1: My background color will change
</div>
{/* Example 2: An input field that will be auto-focused */}
<input ref={focusRef} placeholder="Example 2: I'm focused" />
{/* Example 3: A div that will have its innerHTML changed */}
<div ref={htmlRef}>Example 3: Change my HTML</div>
{/* Example 4: An image that will have its src attribute changed */}
<img ref={imageRef} alt="Example 4" width="150" />
{/* Example 5*/}
<div>
{/*
Example 5
Explanation :
const sectionRef = useRef();: This line creates a ref that we will attach to the section we want to scroll to.
const handleScrollToSection = () => {...}: This function will be triggered when the button is clicked. It uses the .scrollIntoView() method to scroll to the referenced section smoothly.
<button onClick={handleScrollToSection}>Scroll to Section</button>: This button, when clicked, triggers the handleScrollToSection function, causing the page to scroll to the referenced section.
<div ref={sectionRef} style={{ height: '100vh', backgroundColor: 'lightcoral' }}>...: This section is what we will scroll to. It is given a ref attribute, linking it to sectionRef, so that handleScrollToSection knows which element to scroll to. */}
<div>
{/* A button that allows the user to scroll to a specific section */}
<button onClick={handleScrollToSection}>Scroll to Section</button>
{/* Other content */}
<div style={{ height: "100vh", backgroundColor: "lightblue" }}>
Scroll past me!
</div>
{/* The section to scroll to */}
<div
ref={sectionRef}
style={{ height: "100vh", backgroundColor: "lightcoral" }}
>
Hello, I'm the section to scroll to!
</div>
</div>
</div>
</div>
);
};