Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

reactjs - Is drilling down setSate() through multiple components an anti-pattern in React?

I am new with React development and I wonder if this is considered an anti-pattern to pass down setState hook as props.

My code looks like this:

const App = () => {
  const [value, setValue] = useState('')

  return (
    <Component value={value} helpers={{ setValue }} />
  )
}

const Component = (props) => {
  return <Component2 {...props} />
}

const Component2 = (props) => {
  return <Input {...props} />
}

const Input = (props) => {
  const handleChange = (e) => props.helpers.setValue(e.target.value)
  return <input onChange={handleChange}  {...props} />
}

I am working with React-Hook-Form and I need to declare the state at the top level but I still want my handleChange function to be at the component level. However, I don't see this pattern on example codes.

Is it an anti-pattern?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...