1,315 questions
Advice
1 vote
5 replies
95 views
When to Use useState vs Redux for State Management in React
I’m currently working on a React project and I’m trying to determine when to use useState (i.e., local state) versus introducing Redux for managing state. While I understand that Redux can be powerful ...
0 votes
3 answers
225 views
Is it okay to use setState inside a setState callback?
Illustrative example: export const App = () => { const [things, setThings] = useStoredState(window.localStorage, 'things', [ { id: 0, name: 'New Thing' }, ]); const [selectedThingId, ...
-1 votes
1 answer
52 views
Updating an object-like react state does not rerender the component [duplicate]
Imagine a React Component like this with two states, each can be updated with the click of a button: The object-like state foo with the update function setFoo The number state bar with the update ...
2 votes
2 answers
101 views
Optimize Hook Render Times
This is the hook I use to activate the navbar item when its section is visible in the screen, for example: section with id "#howtoorder" is in the visible part of the screen, then the navbar ...
Best practices
1 vote
5 replies
122 views
Where to manage the state of individual items in a list
So, being used other reactive frameworks I'm now starting to learn and work with React. I'm building a simple exercise and I'm struggling with the way State is managed in React, I'll already have ...
0 votes
2 answers
171 views
Updating stateful list in React based on another stateful variable
Here are two React components: function MainScreen() { const [stopwatches, setStopwatches] = useState([ new Stopwatch('A'), new Stopwatch('B') ]); const [selectedStopwatchIndex, ...
2 votes
1 answer
118 views
Component Inside another state doesn't rerender when state changes
The below block of code works. Component rerenders when state changes from parent (MainBox) export function MainBox(){ const [valuestate, setvalueState] = useState() return( <div&...
3 votes
2 answers
252 views
In React, why does useRef need .current but useState doesn't?
Ref interface: // create const count = useRef(0); // update (does not trigger render) count.current = 5; // access console.log(count.current); State variable interface: //create const [count, ...
0 votes
1 answer
95 views
Why doesn't useState update correctly in my custom useFetch hook?
I'm trying to write a custom React hook that fetches data. It seems to work partially — but loading goes false before data is set, and error is never set even when a request fails. import { useState, ...
-1 votes
2 answers
86 views
React Input loses focus after state update
I am trying to build a custom form component. In this I use inertiajs useForm hook to automatically load the data into a state (similar to react-use-form). But my problem is, that everytime i type a ...
0 votes
2 answers
83 views
Not updating state variable which is array of objects
perms is an array of objects. Each object is: { catg: 'Catg1' serv: ->array of serv objects { checked: false, desc: 'Serv 1' } } On button click, based on ...
-1 votes
1 answer
53 views
Why does the prop of the child component change if the parent component has no change?
I have a parent component where I use state to control the visibility of a child component. If I use a button to open a modal it works, but when I change the state in useEffect child prop = false ...
2 votes
1 answer
79 views
setState function not recognized as function
In my React Native app, I'm having a problem where the setState function is not being recognized as a function. I already inserted the context provider on the App root page and exported the context ...
-1 votes
1 answer
60 views
React - component state variable value not updated when used in handler, which passed to child
I try to make some kind of infinite scroll Here is my CodeSandbox setup (first approach) To paginate I made some kind of cursor which keeps last loaded item id in after state variable I use that after ...
0 votes
1 answer
1k views
Shadcn/cmdk cannot bind to command input component
I'm trying to compose my own Combobox component using Shadcn ui. In doing this I am using the Command component. I am trying to bind my command input value to React state in Next.js client component ...