JIYIK CN >

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

All

How to call a function only once in React

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

Use the useEffect hook to call a function only once in React. When the useEffect hook is passed an empty dependencies array, it runs only when the component mounts. This is the preferred approach when we have to fetch data when the component mounts....

Full

Replace objects in array in React state

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

Replace objects in array in React state

To replace an object in an array in React state: Use the map() method to iterate over the array. In each iteration, check if a certain condition is met. Replace the objects that meet the condition and return all other objects as is....

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

Combining multiple inline style objects in React

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

Use spread syntax to combine multiple inline style objects in React, for example style={{...style1, ...style2}} . The spread syntax will unpack the key-value pairs of the object into a final object and the styles will be applied to the element. export...

Full

Functions in React are not valid as a React child error

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

Functions in React are not valid as a React child error

Generates the error Functions are not valid as a React child. This may happen if you return a Component instead of from render. There are 2 common reasons: Returning a function reference instead of a component from render. Using React Router routes as...

Full

Finding and rendering objects in an array using React.js

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

Finding and rendering objects in an array using React.js

To find an object in an array in React: Call the find() method on the array and pass it a function. The function should return an equality check for the relevant properties. The find() method returns the first value in the array that satisfies the con...

Full

Detecting when the Esc key is pressed in React.js

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

Detecting when the Esc key is pressed in React.js

To detect when the Esc key is pressed in React.js: Add a keydown event listener to the document element. When the user presses a key, check if the key is Esc. If the key pressed is Escape, call a function or run some logic. import {u...

Full

Check if browser tab has focus using React

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

Check if browser tab has focus using React

To check if a browser tab has focus using React: Add event listeners for the focus and blur events. If the focus event is fired, the tab has focus. If the blur event is fired, the tab loses focus. import {useE...

Full

How to remove query parameters using React Router

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

How to remove query parameters using React Router

To delete query parameters using React Router: Use the useSearchParams hook to get the search parameters for the current location. Use the delete() method to delete each query parameter, such as searchParams.delete('q'). To update the search parameter...

Full

How to render a boolean value in React JSX

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

How to render a boolean value in React JSX

In React, we use the String() function to render boolean values ​​in JSX, for example, h2{string(bool1)}/h2 . By default, boolean values ​​don’t render anything in React, so we have to convert the value to a string in order to render it. exp...

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

Scan to Read All Tech Tutorials

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

Hottest

Tags

Scan the Code
Easier Access Tutorial