[英]How can I add index as a parameter to useEffect hook?
Well, I am trying to figure out how to play with an array within useEffect hook. 好吧,我试图弄清楚如何在useEffect钩子中使用数组。 Basically, I want to know how to pass index as a parameter so I don't have to grab the indexes manually.
基本上,我想知道如何将索引作为参数传递,因此我不必手动获取索引。
I am fetching some data from the backend in order to edit some form fields. 我从后端获取一些数据,以便编辑一些表单字段。 The field I am having trouble, is a field that you could create dynamically.
我遇到麻烦的领域是一个你可以动态创建的领域。 There could be only 1 or 100.
可能只有1或100。
This is the component: 这是组件:
// HERE IS WHERE I AM ATTEMPTING TO DO WHAT I NEED
useEffect(() => {
// So there is only one name key.
// I need that to be dynamic. To accept as many name keys as possible.
startupFourthStepFormActionHandler({
products_or_services:
[
{
name:
startupProductsOrServicesInfo.productsorservices &&
startupProductsOrServicesInfo.productsorservices[0].name,
},
] || [],
});
}, []);
return (
<div>
{productsOrServicesInputs.map((input, index) => (
<div key={input}>
// THESE ARE DYNAMIC INPUTS. SO THERE COULD 1 OR 100
<FormField
value={startupFourthStepForm.products_or_services[index].name}
/>
// LET'S SAY THIS WAS CREATED DYNAMICALLY
<FormField
value={startupFourthStepForm.products_or_services[index].name}
/>
</div>
))}
</div>
);
};
If you see the component above, on useEffect
I am doing this: 如果你看到上面的组件,在
useEffect
我这样做:
return startupFourthStepFormActionHandler({
products_or_services:
[
{
name:
startupProductsOrServicesInfo.productsorservices &&
startupProductsOrServicesInfo.productsorservices[0].name,
},
] || [],
});
I would like to turn that into something like: 我想把它变成这样的东西:
return startupFourthStepFormActionHandler({
products_or_services:
[
{
name:
startupProductsOrServicesInfo.productsorservices &&
startupProductsOrServicesInfo.productsorservices[INDEX].name,
},
] || [],
});
Or something which allows me to have as many name
keys as needed. 或者允许我根据需要拥有尽可能多的
name
密钥的东西。 Like: 喜欢:
return startupFourthStepFormActionHandler({
products_or_services:
[Here I should grab an array of 100 hundred objects with key called name] || [],
});
So I can do something like: startupProductsOrServicesInfo.productsorservices[index].name
所以我可以做类似的事情:
startupProductsOrServicesInfo.productsorservices[index].name
As you see I have this startupProductsOrServicesInfo.productsorservices[0].name
but I need that be the proepr index of the item in the array. 如你所见,我有这个
startupProductsOrServicesInfo.productsorservices[0].name
但我需要它是数组中项目的proepr索引。 For now it is only grabbing the index 0, I need that index to be grabbed dynamically. 现在它只抓取索引0,我需要动态抓取该索引。
In the useEffect
method on the component, you may see this startupProductsOrServicesInfo.productsorservices
which is an API call and returns this -> 在组件上的
useEffect
方法中,您可能会看到这个startupProductsOrServicesInfo.productsorservices
这是一个API调用并返回此 - >
{
"success": true,
"productsorservices": [
{
"name": "Software",
},
{
"name": "Hardware",
}
]
}
So all I am trying to is to set the value coming from the backend here on this value
-> 所以我所要做的就是在这个
value
上设置来自后端的value
- >
value={startupFourthStepForm.products_or_services[index].name}
which you may in the component <FormField />
. value={startupFourthStepForm.products_or_services[index].name}
,您可以在组件<FormField />
。
I need to do that in the useEffect
hook. 我需要在
useEffect
钩子中这样做。
What am I missing? 我错过了什么?
TL;DR TL; DR
For no the component is doing what I need, but it only grabs the first index of the array, startupProductsOrServicesInfo.productsorservices[0].name
and I need it to be grabbed dynamically. 因为没有组件正在做我需要的,但它只抓取数组的第一个索引,
startupProductsOrServicesInfo.productsorservices[0].name
,我需要动态抓取它。
Fundamentally useEffect
hooks are just a way to control the timing of certain code. 从根本上说,
useEffect
钩子只是一种控制某些代码时序的方法。 Otherwise they're not special at all, and so to solve your problem you just need regular old React solutions. 否则它们根本不是特殊的,因此要解决您的问题,您只需要定期使用旧的React解决方案。
If you want to pass the index into your component, you simply do so with props
: 如果要将索引传递到组件中,只需使用
props
:
const YourComponent = props => {
// HERE IS WHERE I AM ATTEMPTING TO DO WHAT I NEED
useEffect(() => {
// ..
startupProductsOrServicesInfo.productsorservices[props.index].name,
// ..
}
And then in pass the index in in your parent component: 然后传递父组件中的索引:
return <YourComponent index={1} />
If you want your component to remember the index, you have to use state instead: 如果您希望组件记住索引,则必须使用状态:
const YourComponent = () => {
const [index, setIndex] = useState(0);
// HERE IS WHERE I AM ATTEMPTING TO DO WHAT I NEED
useEffect(() => {
// ..
startupProductsOrServicesInfo.productsorservices[index].name,
If you want an event to change that index, for instance if you wanted a button in your component to increment it, you would use the setIndex
function returned by useState
: 如果你想要一个事件改变这种状况指数,例如,如果你在你的组件需要一个按钮来增加它,你可以使用
setIndex
通过返回的函数useState
:
const YourComponent = () => {
const [index, setIndex] = useState(0);
// HERE IS WHERE I AM ATTEMPTING TO DO WHAT I NEED
useEffect(() => {
// ..
startupProductsOrServicesInfo.productsorservices[index].name,
});
const incrementIndex = () => setIndex(index + 1);
return <button onClick={incrementIndex}>Click Me!</button>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.