JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END >

All

Adding Types to Refs in React with TypeScript

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

Adding types to the useRef hook might be a little confusing at first, let’s look at why that is: import {useEffect, useRef} from react ; export function RefDemo () { const inputRef = useRef (); useEffect ( () = { inputRef. current . fo...

Full

Qualify useState as an object in React TypeScript

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

To qualify the useState hook object in React, use the hook's generic type, for example, const [employee, setEmployee] = useState{name: string; salary: number}({name: ,salary: 0}) . The state variable will only accept key-value pairs of the specified t...

Full

How to cross out (strikethrough) text in React

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

How to cross out (strikethrough) text in React

Use the textDecoration property to add a strikethrough to text in React, for example, span style={{textDecoration: line-through}} . The text-decoration CSS property sets the appearance of text decoration lines. const App = () = { return ( div h2 span ...

Full

Return to the previous page using React Router

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

Return to the previous page using React Router

To go back to the previous page with React Router: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Calls the navigate() function passed to it -1 - navigate(-1) . Calling navigate with -1 is the same as clicking the back button. impo...

Full

Open file input box on button click in React

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

Open file input box on button click in React

To open a file input on button click in React: Set the onClick attribute on the button element. Set the ref attribute on the file input. When the button is clicked, open the file input, e.g. inputRef.current.click() . App.js import { useRef } from...

Full

Cross out (strikethrough) text on click in React

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

Cross out (strikethrough) text on click in React

To cross out text on click in React: Set an onClick attribute on the element. When the element is clicked, check if its text-decoration attribute is set. If the attribute is set, remove it, otherwise set it to line-through. const App =...

Full

Creating a numbers-only input field in React.js

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

Creating a numbers-only input field in React.js

To create an input field in React.js that only contains numbers: Set the input field's type to text . Add an onChange event handler that removes all non-numeric values ​​. Save the input value in a state variable. import { useState } from reactjs...

Full

Get the mouse position (coordinates) in React

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

Get the mouse position (coordinates) in React

To get the mouse position in React: Set the `onMouseMove` property on the element or add an event listener on the window object. Provide an event handler function. Access the relevant properties of the event object....

Full

Ref returns undefined or null in React

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

React refs most often return undefined or null when we try to access their current properties before the corresponding DOM element is rendered. To fix this, you need to access the ref in the useEffect hook or when an event is triggered. import {useRef...

Full

React error Unexpected default export of anonymous function

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

Unexpected default export of anonymous function warning is caused when we try to export an anonymous function using default export. To fix the error, name the function before exporting it. Below is a sample code Header.js that produces the above error...

Full

Type is not assignable to type 'never' error in React

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

When we use the useState hook to declare an empty state array but do not type the array, we get the error Type is not assignable to type never. To fix the error, use generics to type the state array, such as const [arr, setArr] = useStatestr...

Full

Prevent page refresh when submitting form in React.js

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

Prevent page refresh when submitting form in React.js

To prevent a page refresh when a form is submitted in React, use the preventDefault() method on the event object, for example event.preventDefault() . The preventDefault() method prevents the browser from taking the default action, which in the case o...

Full

Nothing was returned from render error in React

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

The Nothing was returned from render React error occurs when we forget to explicitly return a value from a function or class component. To fix the error, explicitly return JSX from our component, use implicit returns with arrow functions, or...

Full

Creating N identical components/elements in React

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

To create N identical components/elements in React: Use Array.from() method to generate an array of N elements. Replace each element in the array with the actual component. Render the array of elements. function Header () { return h2 Hello world / h2 ...

Full

Attempted import error 'X' is not exported from error in React

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

React.jsAttempted import error X is not exported from appears when we try to import a named import that does not exist in the specified file. To resolve the error, make sure the module has named exports and that we are not mixing named exports with de...

Full

Scan to Read All Tech Tutorials

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

Hottest

Tags

Scan the Code
Easier Access Tutorial