javascript - Multiple React effect hooks for the same dependency - Stack Overflow

Let's say I have an effect hook with a Person dependency that follows the schema Person: {name: st

Let's say I have an effect hook with a Person dependency that follows the schema Person: {name: string, age: number}. My effect hook for this Person dependency currently looks like this:

useEffect(() => {
  if (person.name === 'Mike') {
    doSomething()
  }

  if (person.age > 21) {
    somethingElse()
  }
}, [person])

Would it be valid code to separate this logic into their own effect hooks with the same dependencies:

// effect that handles any logic for a person's name
useEffect(() => {
  if (person.name === 'Mike') {
    doSomething()
  }
}, [person])

// effect that handles any logic for a person's age
useEffect(() => {
  if (person.age > 21) {
    somethingElse()
  }
}, [person])

I'm trying to separate unrelated code from each other in some of my ponents, and I'm wondering if this would be considered an anti-pattern or if it could result in unwanted issues?

Let's say I have an effect hook with a Person dependency that follows the schema Person: {name: string, age: number}. My effect hook for this Person dependency currently looks like this:

useEffect(() => {
  if (person.name === 'Mike') {
    doSomething()
  }

  if (person.age > 21) {
    somethingElse()
  }
}, [person])

Would it be valid code to separate this logic into their own effect hooks with the same dependencies:

// effect that handles any logic for a person's name
useEffect(() => {
  if (person.name === 'Mike') {
    doSomething()
  }
}, [person])

// effect that handles any logic for a person's age
useEffect(() => {
  if (person.age > 21) {
    somethingElse()
  }
}, [person])

I'm trying to separate unrelated code from each other in some of my ponents, and I'm wondering if this would be considered an anti-pattern or if it could result in unwanted issues?

Share Improve this question asked Feb 27, 2020 at 21:24 Mike MaliszMike Malisz 1032 silver badges8 bronze badges 2
  • 1 no, that's fine. – Jonas Wilms Commented Feb 27, 2020 at 21:27
  • 2 Side note: you could specify the object properties as dependencies instead of the entire object. E.g. [person.name] and [person.age] – Andy Mardell Commented Feb 27, 2020 at 21:31
Add a ment  | 

2 Answers 2

Reset to default 5

You are correct. Although, I would check person properties on each individual effect call separately. Check the react docs here for a good example. ( I really dislike when others are answering and I'm still typing...)

 // effect that handles any logic for a person's name
useEffect(() => {
  if (person.name === 'Mike') {
    doSomething()
  }
}, [person.name])

// effect that handles any logic for a person's age
useEffect(() => {
  if (person.age > 21) {
    somethingElse()
  }
}, [person.age])

While it is acceptable to write code in this fashion, you might want to run the effects on what they're actually concerned with. ie

// effect that handles any logic for a person's name
useEffect(() => {
  if (person.name === 'Mike') {
    doSomething()
  }
}, [person.name])

// effect that handles any logic for a person's age
useEffect(() => {
  if (person.age > 21) {
    somethingElse()
  }
}, [person.age])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796521a4594235.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信