I have some data (objects) that live in their own file (Origin.js).
This data is being exported using the spread operator within another object named OriginState
:
Origin.js
//info
const info = {
title: '',
year: '',
};
//images
const images = {
logo: '',
heroImage: '',
};
//text
const text = {
header: '',
body: '',
};
export const OriginState = {
...info,
...images,
...text,
};
I am importing my OriginState
object in another file and using it as state
for my app like this:
OtherFile.js
import { OriginState } from './Origin.js';
const [state, setState] = useState({
...OriginState,
});
Here is an example handler where I am using this state to update some specified state values in an input later:
const handleChange = (e) => {
const { name, value } = e.target;
setState((state) => ({
...state,
[name]: value,
}));
};
Now, my question is… Is it incorrect to store state
like this?
Additionally, am I using setState
incorrectly in my handler function?
In most cases I’ve seen state declared and updated like this which is obviously easier to read:
const [count, setCount] = useState(0);
setCount(count + 1)
But I have a lot of state
and didn’t think it would be a good idea to have multiple setState
hooks.
Is there a better way to do this? What I currently have just feels wrong.
Your approach seems quite legit, and this is one of the best practices that if your
newState
is depend on theoldState
usesetState callback
and get the old state fromcallback input
because otherwise as you showed above if you use it like this:you may increase the chance to get
stale data
which will increase the potential for bugNope, not at all, in fact, it is often the preferable pattern for state updates.
Any time your state update depends on the previous state, i.e. the classic counter example, or in your case, when there is nested state, you should use a functional state update to update from the previous state instead of the state from the previous render cycle.
I see no issue with your state update logic in the handler. In this
count
example it would (should) be considered incorrect to update a count like this. See this codesandbox demo that attempts to show the issue between non-functional and functional state updates.The correct state update should be
setCount(count => count + 1)
When it comes to form inputs and state I think it makes sense to have a single flat object. There isn’t really a right or wrong answer in general though when it comes to using a single
useState
hook with "complex" state shape, or to use a singleuseState
hook for each "chunk" of state. It’s an opinionated answer, mostly do what makes sense for a specific use-case.Generally though I’d say if a set of values are even loosely related then perhaps it makes sense to store them in a common object, but this is my opinion.
A potential issue I see with your imported data though is the chance that you may inadvertently overwrite some key-value pairs by the use of the Spread syntax.
buy real viagra no prescription
buy generic zofran
Iathdy – http://virviaga.com/ viagra or cialis
alprostadil penile suppository https://alprostadildrugs.com/ trimix vs alprostadil
Dpyqch – pharmacie en ligne belgique tadalafil Hznywr cwyann
sildenafil 6mg https://eunicesildenafilcitrate.com/ sildenafil 25 mg tablet
vardenafil 40 mg generic https://vegavardenafil.com/ where to buy online vardenafil with out prescribed
tadalis sx https://elitadalafill.com/ tadalafil dosage
Zdasyp – zithromax Pgphgi eejoce
Cqdulx – compare cialis prices online Sheqit rfdbir
Iuyrfx – viagra prescription uk Vlnwej ltsytn
Fxicmq – cut finasteride 5mg Ghwjii wnwheo
Uxemky – tadacip 10 mg Bbemlh hrlpqv
Iaivkb – avana pills Ahamve wtevvq