What do you want to save?
Add Code snippet
New code examples
-
Javascript 2022-03-27 07:10:04
useffect compare previous value to current
//Custom Hook function usePrevious(value) { const ref = useRef(); useEffect(() => { ref.current = value; }); return ref.current; } // Use it in useEffect const Component = (props) => { const {receiveAmount, sendAmount } = props ... Add solution -
Javascript 2022-03-16 18:05:04
react useeffect common dependency
import React, { useState, useRef, useEffect } from "react"; function EffectsDemoNoDependency() { const [title, setTitle] = useState("default title"); const titleRef = useRef(); useEffect(() => { console.log("useEffect... Add solution -
Javascript 2022-02-18 16:30:02
settimeout react native focus text input
const inputRef = useRef(null) In onSubmitEditing prop, include this: setTimeout(() => inputRef.current.focus(), 250); Ex: const onEnterKeyPressed = () => { ... setTimeout(() => inputRef.current.focus(), 100); ... } <TextInput ...... Add solution -
Javascript 2022-02-01 02:10:25
scrollbar automatically scroll down as new divs are added reactjs
import React, { useEffect, useRef } from 'react' const Messages = ({ messages }) => { const messagesEndRef = useRef(null) const scrollToBottom = () => { messagesEndRef.current.scrollIntoView({ behavior: "smooth" }) } useEff... Add solution -
Javascript 2021-11-17 09:34:36
call a method of component from outside react
const MyComponent = ({myRef}) => { const handleClick = () => alert('hello world') myRef.current.handleClick = handleClick return (<button onClick={handleClick}>Original Button</button>) } MyComponent.defaultProps = { myRef: {c... Add solution -
Javascript 2021-11-11 08:22:11
useRef example
/* A common use case is to access a child imperatively: */ function TextInputWithFocusButton() { const inputEl = useRef(null); const onButtonClick = () => { // `current` points to the mounted text input element inputEl.current.focus(); ... Add solution -
Other 2021-11-08 04:42:10
how to not execute useEffect when loading the page first time
//This is a way to build this effect as a custom hook import React, { useEffect, useRef } from 'react'; const useDidMountEffect = (func, deps) => { const didMount = useRef(false); useEffect(() => { if (didMount.current) func(); ... Add solution