[英]React - What's the best way of passing a state setter function to a helper function from a component?
For context, I'm new to React, in case this is something commonly asked.对于上下文,我是 React 的新手,以防这是常见问题。 I'm still getting used to all the terminology, so my apologies if anything sounds confusing.
我仍然习惯了所有的术语,所以如果有任何令人困惑的地方,我深表歉意。
Say there is a component in which we make use of the state hook.假设我们在一个组件中使用了 state 钩子。 Let's say:
比方说:
function Example() {
const [count, setCount] = useState(0);
const [showTable, setShowTable] = useState(false);
// some logic follows here
// and some rendering
}
If I don't want to smush all logic into the component, it would make sense to create some helper functions.如果我不想将所有逻辑都混入组件中,那么创建一些辅助函数是有意义的。 Each one gets its own file.
每个人都有自己的文件。 And the "useEffect" hook gets a file of its own as well.
“useEffect”钩子也有自己的文件。 Something like:
就像是:
import useInitExample from './hooks/useInitExample';
function Example() {
const [count, setCount] = useState(0);
const [showTable, setShowTable] = useState(false);
const someParam = {
...
}
useInitExample(someParam);
// rendering happening here
}
useInitExample is importing and using a bunch of the aforementioned helper functions. useInitExample 正在导入和使用一堆上述的辅助函数。
Now, assuming that a helper function needs to either call a setter, or needs to know a current state, I assume we need to pass it along from the main component to the useInitExample file, and from there to the helper functions.现在,假设帮助程序 function 需要调用一个设置器,或者需要知道当前的 state,我假设我们需要将它从主组件传递到 useInitExample 文件,然后从那里传递到辅助函数。 So something like this:
所以是这样的:
import useInitExample from './hooks/useInitExample';
function Example() {
const [count, setCount] = useState(0);
const [showTable, setShowTable] = useState(false);
const someParam = {
count,
setCount,
showTable
}
useInitExample(someParam);
// rendering happening here
}
and subsequently, in useInitExample.js, I would need to do something like:随后,在 useInitExample.js 中,我需要执行以下操作:
import helperFunction from './../helpers/helperFunction'
import anotherHelper from './../helpers/anotherHelper'
function useInitExample(someParam) {
const result = helperFunction({
count: someParam.count,
setCount: someParam.setCount
})
const result2 = anotherHelper({
count: someParam.count,
showTable: someParam.showTable
})
...
}
Aside from the example above not making much actual sense, with a complex component, this approach can obviously get out of hand fast, with passing a ton of things between functions, and things can get cluttered.除了上面的例子没有多大实际意义之外,对于一个复杂的组件,这种方法显然会很快失控,在函数之间传递大量的东西,事情会变得混乱。 Especially if a helper function makes use of further helper functions.
特别是如果帮助器 function 使用更多的帮助器功能。
I've been looking at alternative ways to structure this.我一直在寻找替代方法来构建它。 So far, I've been looking at Context, but from what I can tell, that only helps with nested components, not helper functions.
到目前为止,我一直在研究 Context,但据我所知,这只对嵌套组件有帮助,对辅助函数没有帮助。
I feel dirty about using redux to solve this.我觉得用 redux 来解决这个问题很肮脏。 I also thought about passing all setters and states to every function as a parameter, but that also feels dirty.
我还考虑将所有设置器和状态作为参数传递给每个 function,但这也感觉很脏。
I also tried isolating all instances of useState() for a component into a separate file to be imported by said helper functions and the component as well, but alas, useState() doesn't work outside of a component (which makes sense).我还尝试将组件的所有 useState() 实例隔离到一个单独的文件中,以便由所述辅助函数和组件导入,但是可惜,useState() 在组件之外不起作用(这是有道理的)。
Is there any way to avoid having to pass setters and states in the way that I describe above?有什么方法可以避免以我上面描述的方式传递设置器和状态?
Before you ask, the helper functions described above are specific to a component and not intended to be reused by other components.在你问之前,上面描述的帮助函数是特定于一个组件的,不打算被其他组件重用。
Thanks!谢谢!
in case which the helper functions described above are specific to a component and not intended to be reused by other components, it indeed means you have a lot of logic to write and there is no one to minimize it or structure it differently.如果上面描述的辅助函数是特定于一个组件并且不打算被其他组件重用,这确实意味着你有很多逻辑要编写并且没有人可以最小化它或以不同的方式构造它。 if you want the code less verbose i might suggest to receive to parameters to
useInitExample
as destruct object and then you can pass them like:如果您希望代码不那么冗长,我可能会建议将参数接收到
useInitExample
作为 destruct object 然后您可以像这样传递它们:
function useInitExample({ count, setCount }) {
const result = helperFunction({ count, setCount })
const result2 = anotherHelper({ count, setCount })
...
}
in case the state should be shared in several components/hooks you can use React.Context , it indeed a great and elegant solution.如果 state 应该在多个组件/挂钩中共享,您可以使用React.Context ,它确实是一个伟大而优雅的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.