get current useState values inside functions (w/ short, explicit codepen) - reactjs

I understand (somewhat) how to use useEffect to update state, but I struggle with situations like when you need current state inside of another function, before the "nextTick" as it were.
Here is a simple Codepen with the exact issue. Make sure the Pen console is open.
https://codepen.io/kirkbross/pen/vYRNpqG?editors=1111
const App = () => {
const [state, setState] = React.useState(null);
// how can I make sure the below function knows what the current state really is?
const handleAppend = (state) => {
console.log("click");
console.log(state?.text + " foobar");
};
return (
<div class="app">
<div className="row">
<span>Text: </span>
<input
type="text"
onChange={() => setState({ text: e.target.value })}
/>
</div>
<div className="row">
<button onClick={handleAppend}>
Append "foobar" to text and log it to console
</button>
</div>
</div>
);
};

You're shadowing your state variable in your handleAppend function. You don't need to pass in an argument since state is available in scope of the component
const handleAppend = () => {
console.log("click");
console.log(state?.text + " foobar");
};

I did some changes. You dont need to use ur state as a parameter, since your textState lives inside your app component and there for you can reach it within your function.
Also, i changed the state and setState to textState, setTextState to make it less confusing. Also after clicking on the button and console logging, i cleared the textState so the next value wont be effected. Check it out below.
function App() {
const [textState, setTextState] = React.useState(null);
const handleAppend = () => {
console.log("click");
console.log(textState + " foobar");
setTextState('')
//also, you could make the input box clear after each press on button by adding value={textState} in the input.
};
return (
<div className="App">
<input
type="text"
onChange={(e) => setTextState(e.target.value)}
/>
<button onClick={handleAppend}>
Append "foobar" to text and log it to console
</button>
</div>
);
}

My real world case was more complicated than the Pen. The actual function needing state was a useCallback function and I had forgotten to add state to the dep array of the useCallback function.
const handleDragEnd = useCallback(
async (result) => {
const { source, destination, draggableId } = result;
console.log(state); // shows up now.
},
[state], // I had forgotten to add state to the useCallback dep array
);

Related

Why useRef value dynamically updates when it is an integer but stores the previous value when it is a string?

I just learned useRef and am confused about how it actually works. For example,
#1
function Ref() {
const rerenderCount = useRef(0);
useEffect(() => {
rerenderCount.current = rerenderCount.current + 1;
});
return <div>{rerenderCount.current}</div>;
}
Here, the useRef gives the same output as useState in the following code, #2
function State() {
const [rerenderCount, setRerenderCount] = useState(0);
useEffect(() => {
setRerenderCount(prevCount => prevCount + 1);
});
return <div>{rerenderCount}</div>;
}
But in this #3 code, the previousName.current value always displays the previous value. But it is set to name.
const [name, setName] = useState("");
const previousName = useRef(null);
useEffect(() => {
previousName.current = name;
}, [name]);
return (
<div className="App">
<input value={name} onChange={(e) => setName(e.target.value)} />
<div>
My name is {name} and it used to be {previousName.current}
</div>
</div>
);
Please, someone explain why the name is one step back where the integer updates on time. Also, what is the use of [name] in useEffect. Without or without it, I get the same result and rendercount.
In you example previousName is one step behind because when you change the name state the component is re-render, useEffect is called and previousName is updated but this last change doesn't cause a new rendering (useRef is not like useState, the component is not re-render), so you see name updated correctly but previousName with the same value as before, even if its value has changed.
This is because the change of previousName occurs during the subsequent rendering caused by the changing on the state.
To see its change an additional rendering would require.
To avoid this behavior you could use an event handler and not rely on the useEffect hook.
const handleChange = (text: string) => {
setName(text);
previousName.current = text;
};
return (
<div className="App">
<input value={name} onChange={(e) => handleChange(e.target.value)} />
<div>
My name is {name} and it used to be {previousName.current}
</div>
</div>
);

React - UseEffect not re-rendering with new data?

This is my React Hook:
function Student(props){
const [open, setOpen] = useState(false);
const [tags, setTags] = useState([]);
useEffect(()=>{
let input = document.getElementById(tagBar);
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById(tagButton).click();
}
});
},[tags])
const handleClick = () => {
setOpen(!open);
};
function addTag(){
let input = document.getElementById(tagBar);
let tagList = tags;
tagList.push(input.value);
console.log("tag");
console.log(tags);
console.log("taglist");
console.log(tagList);
setTags(tagList);
}
const tagDisplay = tags.map(t => {
return <p>{t}</p>;
})
return(
<div className="tags">
<div>
{tagDisplay}
</div>
<input type='text' id={tagBar} className="tagBar" placeholder="Add a Tag"/>
<button type="submit" id={tagButton} className="hiddenButton" onClick={addTag}></button>
<div>
);
What I am looking to do is be able to add a tag to these student elements (i have multiple but each are independent of each other) and for the added tag to show up in the tag section of my display. I also need this action to be triggerable by hitting enter on the input field.
For reasons I am not sure of, I have to put the enter binding inside useEffect (probably because the input element has not yet been rendered).
Right now when I hit enter with text in the input field, it properly updates the tags/tagList variable, seen through the console.logs however, even though I set tags to be the re-rendering condition in useEffect (and the fact that it is also 1 of my states), my page is not updating with the added tags
You are correct, the element doesn't exist on first render, which is why useEffect can be handy. As to why its not re-rendering, you are passing in tags as a dependency to check for re-render. The problem is, tags is an array, which means it compares the memory reference not the contents.
var myRay = [];
var anotherRay = myRay;
var isSame = myRay === anotherRay; // TRUE
myRay.push('new value');
var isStillSame = myRay === anotherRay; // TRUE
// setTags(sameTagListWithNewElementPushed)
// React says, no change detected, same memory reference, skip
Since your add tag method is pushing new elements into the same array reference, useEffect thinks its the same array and is not re-triggers. On top of that, React will only re-render when its props change, state changes, or a forced re-render is requested. In your case, you aren't changing state. Try this:
function addTag(){
let input = document.getElementById(tagBar);
let tagList = tags;
// Create a new array reference with the same contents
// plus the new input value added at the end
setTags([...tagList, input.value]);
}
If you don't want to use useEffect I believe you can also use useRef to get access to a node when its created. Or you can put the callback directly on the node itself with onKeyDown or onKeyPress
I can find few mistake in your code. First, you attaching event listeners by yourself which is not preferred in react. From the other side if you really need to add listener to DOM inside useEffect you should also clean after you, without that, another's listeners will be added when component re-rendered.
useEffect( () => {
const handleOnKeyDown = ( e ) => { /* code */ }
const element = document.getElementById("example")
element.addEventListener( "keydown", handleOnKeyDown )
return () => element.removeEventListener( "keydown", handleOnKeyDown ) // cleaning after effect
}, [tags])
Better way of handling events with React is by use Synthetic events and components props.
const handleOnKeyDown = event => {
/* code */
}
return (
<input onKeyDown={ handleOnKeyDown } />
)
Second thing is that each React component should have unique key. Without it, React may have trouble rendering the child list correctly and rendering all of them, which can have a bad performance impact with large lists or list items with many children. Be default this key isn't set when you use map so you should take care about this by yourself.
tags.map( (tag, index) => {
return <p key={index}>{tag}</p>;
})
Third, when you trying to add tag you again querying DOM without using react syntax. Also you updating your current state basing on previous version which can causing problems because setState is asynchronous function and sometimes can not update state immediately.
const addTag = newTag => {
setState( prevState => [ ...prevState, ...newTage ] ) // when you want to update state with previous version you should pass callback which always get correct version of state as parameter
}
I hope this review can help you with understanding React.
function Student(props) {
const [tags, setTags] = useState([]);
const [inputValue, setInputValue] = useState("");
const handleOnKeyDown = (e) => {
if (e.keyCode === 13) {
e.preventDefault();
addTag();
}
};
function addTag() {
setTags((prev) => [...prev, inputValue]);
setInputValue("");
}
return (
<div className="tags">
<div>
{tags.map((tag, index) => (
<p key={index}>{tag}</p>
))}
</div>
<input
type="text"
onKeyDown={handleOnKeyDown}
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Add a Tag"
/>
<button type="submit" onClick={addTag}>
ADD
</button>
</div>
);
}

Incorrect use of useEffect() when filtering an array

I have this React app that's is getting data from a file showing in cards. I have an input to filter the cards to show. The problem I have is that after I filter once, then it doesn't go back to all the cards. I guess that I'm using useEffect wrong. How can I fix this?
import { data } from './data';
const SearchBox = ({ onSearchChange }) => {
return (
<div>
<input
type='search'
placeholder='search'
onChange={(e) => {
onSearchChange(e.target.value);
}}
/>
</div>
);
};
function App() {
const [cards, setCards] = useState(data);
const [searchField, setSearchField] = useState('');
useEffect(() => {
const filteredCards = cards.filter((card) => {
return card.name.toLowerCase().includes(searchField.toLowerCase());
});
setCards(filteredCards);
}, [searchField]);
return (
<div>
<SearchBox onSearchChange={setSearchField} />
<CardList cards={cards} />
</div>
);
}
you should Include both of your state "Card", "searchedField" as dependincies to useEffect method.once any change happens of anyone of them, your component will re-render to keep your data up to date,
useEffect(() => { // your code }, [searchField, cards]);
cards original state will be forever lost unless you filter over original data like const filteredCards = data.filter().
though, in a real project it's not interesting to modify your cards state based on your filter. instead you can remove useEffect and create a filter function wrapped at useCallback:
const filteredCards = useCallback(() => cards.filter(card => {
return card.name.toLowerCase().includes(searchField.toLowerCase());
}), [JSON.stringify(cards), searchField])
return (
<div>
<SearchBox onSearchChange={setSearchField} />
<CardList cards={filteredCards()} />
</div>
);
working example
about array as dependency (cards)
adding an object, or array as dependency at useEffect may crash your app (it will throw Maximum update depth exceeded). it will rerun useEffect forever since its object reference will change everytime. one approach to avoid that is to pass your dependency stringified [JSON.stringify(cards)]

Hooks not setting the state in REACT

In the following code, the hook shown below does not change the state of the variable newName.
import React, { useState } from 'react'
const App = () => {
const [ persons, setPersons] = useState([
{ name: 'Arto Hellas' }
])
const [ newName, setNewName ] = useState('')
const textChangeHandler = (event)=>{
event.preventDefault()
setNewName(event.target.value) // WORKS FINE
}
const submitHandler = (event)=>{
event.preventDefault()
let temp = {name:newName}
setNewName('') //////////////////////////////////////// PROBLEM - doesnot set state!!
console.log('tenp name is',temp.name)
console.log('new name is',newName)
setInterval(()=>console.log("Set Interval",newName), 1000)
}
return (
<div>
<h2>Phonebook</h2>
<form onSubmit={submitHandler}>
<div>
name: <input onChange={textChangeHandler} />
</div>
<div>
<button type="submit" >add</button>
</div>
</form>
<h2>Numbers</h2>
{persons.map((person) => <Person key = {person.name} name={person.name}/> )}
</div>
)
}
const Person = ({name})=> <p> {name} </p>
setNewName works fine and the name is updated when anything is typed in the input box. However, when I submit button, the setNewName does not seem to work. Even after updating, executing the setNewName the name is still the old name.
I have even tried a setInterval (thinking it may be due to asynchronous nature of JS) and printed the newName but, it still shows the old name.
What is the problem and how can it be fixed?
thanks
If you look at your submitHandler function, you will notice that the name its self has not changed... it remains empty as in its initial state. React only reload if the state value changes as shown below
const submitHandler = (event)=>{
event.preventDefault()
let temp = {name:newName}
//setNewName('') // your value is not changing here ... removing line
setNewName(temp.name) // changing the name here with the one in temp
console.log('tenp name is',temp.name)
console.log('new name is',newName)
setInterval(()=>console.log("Set Interval",newName), 1000)
}
As you know setting a state is asynchronous. Never use scheduling functions like setTimeout setInterval to log out the value of state.
Use useEffect instead -
useEffect(() => {
console.log("value is", newName);
}, [newName]);
You hook is working fine without a problem. Here's a codeSandbox showing everything's working fine.
In React setState is not synchronous ( with class components and hooks ).
So you can not expect that a new state will be available immediately.
You need to change the implementation of the submitHandler function.

Debounce not working when setting state inside of it

A component that preforms autocomplete.
when typing an API request is sent so I added a debouncer.
When setting the inputValue inside the debouncer the debouncer doesn't preform.
const SearchComp = ({
autoCompleteRes,
classes,
currCondtionsForSelectedAction,
forecastForSelectedAction,
searchAction,
}) => {
const [ inputValue, setInputValue] = useState('Tel aviv')
const changeText = (e) => {
const searchTerm = e.target.value.trim()
if( searchTerm === ('' || undefined)) {
clearSearchAction()
return
}
searchAction(searchTerm)
}
const debounce = (func) => {
let debouncerTimer;
return function(e){
setInputValue(e.target.value) // if i comment this line
const context = this;
const args = arguments;
clearTimeout(debouncerTimer);
e.persist()
debouncerTimer = setTimeout(() => {
return func.apply(context,args)},1500)
}
}
const onClickedRes = (selected) => {
setInputValue(`${selected.LocalizedName}, ${selected.AdministrativeArea.LocalizedName} ${selected.Country.LocalizedName}`)
forecastForSelectedAction(selected);
currCondtionsForSelectedAction(selected);
}
return (
<div className={classes.root}>
<div className={classes.inputWrapper}>
<input type="text" className={classes.inputStyle} name="firstname"
value={inputValue} // and comment this line the debouncer works
onChange={debounce(changeText)}
/>
<div className={classes.dropDownContent}>
{autoCompleteRes.map(item => (
<div
key={item.Key}
className={classes.autoCompleteSingleRes}
onClick={() => onClickedRes(item) }
>
{`${item.LocalizedName}, ${item.AdministrativeArea.LocalizedName} ${item.Country.LocalizedName}`}
</div>))}
</div>
</div>
</div>
)
;}
Instead of one call to the changeText function I call every keyboard stroke.
not sure what's going on and how to solve it.
Thanks
By having your debounce function inside of your Functional Component, it is recreating the function on every render (each keystroke would cause a re-render), and applying the newly created debounce function to your changeText.
There are a couple of approaches you could take here:
1) Move the debounce function outside of your component so it is idempotent between renders. This means you put setInputValue and such in to the func argument you pass to your debounce, as they are now not in scope.
2) Wrap your debounce function in a React.useCallback which will memoize the debounce so it does not change between renders unless the dependencies it relies upon change (setinputValue).

Resources