JIYIK CN >

Current Location:Home > Learning >

All

Passing components as props in React

Publish Date:2025/03/13 Author:JIYIK Category:React

We can use the built-in children property to pass components as props in React. All the elements we pass between the opening and closing tags of the component are assigned to the children property....

Full

Showing hover elements in React

Publish Date:2025/03/13 Author:JIYIK Category:React

Showing hover elements in React

To show an element on hover in React: Set `onMouseOver` and `onMouseOut` properties on the element. Track whether the user is hovering over the element in a state variable. Conditionally render other elements based on the state variable....

Full

How to set target=_blank in React

Publish Date:2025/03/13 Author:JIYIK Category:React

How to set target=_blank in React

To set the target attribute of an element to _blank in React, use an anchor element and set the rel attribute, for example . The _blank value indicates that the resource is loaded in a new tab....

Full

Open links in new tabs with React

Publish Date:2025/03/13 Author:JIYIK Category:React

Open links in new tabs with React

To open a link in a new tab in React, use an a element and set its `target` attribute to _blank, for example. A _blank value indicates that the resource is loaded into the new tab....

Full

Generate a random number in React

Publish Date:2025/03/13 Author:JIYIK Category:React

Generate a random number in React

Generate a random number in React using the Math.random() function, for example, Math.floor(Math.random() * (max - min + 1)) + min. The Math.random function returns a number in the range of 0 to less than 1, but can also be used to generate numbers in...

Full

Sorting an array of objects in React

Publish Date:2025/03/13 Author:JIYIK Category:React

To sort an array of objects in React: Create a shallow copy of the array. Call the array's `sort()` method, passing it a function. The function is used to define the sort order....

Full

Toggle a class on click in React

Publish Date:2025/03/13 Author:JIYIK Category:React

Toggle a class on click in React

To toggle a class on click in React: Set an onClick attribute on the element. Store the active state in a state variable. Use a ternary operator to conditionally add a class....

Full

Setting data attributes in React

Publish Date:2025/03/13 Author:JIYIK Category:React

Setting data attributes in React

To set a data attribute for an element in React, set the attribute directly on the element, such as or use the setAttribute() method, such as el.setAttribute('data-foo', 'bar'). We can access the element on the event object or...

Full

How to reverse an array in React

Publish Date:2025/03/13 Author:JIYIK Category:React

Reverse an array in React: Create a shallow copy of the array using the spread syntax `(...)`. Call the `reverse()` method on the copy. Store the result in a variable....

Full

Get the width of an element in React

Publish Date:2025/03/12 Author:JIYIK Category:React

To get the width of an element in React: Set a ref attribute on the element. In the useLayoutEffect hook, update the state variable for the width. Use the offsetWidth attribute to get the width of the element....

Full

How to get the current year in React

Publish Date:2025/03/12 Author:JIYIK Category:React

How to get the current year in React

Get the current year in React using the Date() constructor, for example, new Date().getFullYear(). The getFullYear() method will return a number corresponding to the current year....

Full

Change button text on click in React

Publish Date:2025/03/12 Author:JIYIK Category:React

Change button text on click in React

To change the text of a button when clicked in React: Track the text of the button in a state variable. Set an onClick attribute on the button element. When the button is clicked, update the state variable....

Full

Using Ref to set input value in React

Publish Date:2025/03/12 Author:JIYIK Category:React

Using Ref to set input value in React

To set the value of an input field using ref in React: Set the ref attribute on the input element. When an event is triggered, update the value of the ref. For example, ref.current.value = 'New value'....

Full

Get the width and height of the window in React

Publish Date:2025/03/12 Author:JIYIK Category:React

Get the width and height of the window in React

To get the width and height of the window in React: Use the innerWidth and innerHeight properties of the window object. Add an event listener for the resize event in the useEffect hook. Save the changes in the window width and height in state variable...

Full

Redirecting to an external URL in React

Publish Date:2025/03/12 Author:JIYIK Category:React

Use the window.location.replace() method to redirect to an external url in React, for example, window.location.replace('https://www.jiyik.com'). We can programmatically replace the current resource with a new one by calling the replace() method if a c...

Full

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Hottest

Tags

Scan the Code
Easier Access Tutorial