1
Hello, everybody I’m starting with Reactjs Could someone ask a question, please?
I was introduced to the two ways of creating components (of classes and functions), and chose to use ALWAYS functions (at least for now) Even on App.js, since we can now manipulate states via Hooks, right? hahahahaha
Well, I’m still a little confused about useState, so get this:
Apparently, when we use classes, the old States are merged with the new ones, so we keep the old ones intact, right... But with functional components and useState, we need to worry about all states And that’s my biggest wrap huahuhahhauauh
Since we have to individually manage each state, would it be better to create several and several, and so take care of each one separately? Which is the most suitable Pattern?
I’ll send you a little bit of code:
const App = () => {
const [ personsState, setPersonsState ] = useState({
persons: [
{name: 'Max', age: 28 },
{name: 'Eddie', age: 20 },
{name: 'Julie', age: 25 }
],
showPersons: false
});
const inputNameHandler = (event) => {
setPersonsState({
persons: [
{name: personsState.persons[0].name, age: personsState.persons[0].age },
{name: event.target.value, age: personsState.persons[1].age },
{name: personsState.persons[2].name, age: personsState.persons[2].age }
]
});
}
// minha maior dúvida é aqui
// eu deveria ter criado outro useState só para o showPerson?
// esta seria a melhor opção para manter os states antigos?
const togglePersonsHandler = () => {
const showing = personsState.showPersons;
setPersonsState({
persons: personsState.persons,
showPersons: !showing
});
}
From now on, thank you guys!
You do
persons
andshow
separated.– novic