最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How can I pass the index from javascript map function to the onclick function? - Stack Overflow

programmeradmin2浏览0评论

I have an array of data that I'm iterating through, In the javascript map() function call I'm using the second argument that map() takes, 'index'. How would I pass index to the onClick event? I tried adding index to the list of arguments for onClick but all I can access are the events. Is there anyway I can pass the index through?

I'd like to be able to execute the mented out line 'handleImageClick(productObjects[index])'. I need to let the parent ponent know which productObject was clicked on by the user.

export default function SideProducts(props) {
    
const { productObjects, handleImageClick } = props;


const onClick = (e) => {
    e.persist()
    console.log(e)
    //handleImageClick(productObjects[index])
}

console.log(productObjects)
    return (
        <Wrapper>
            {productObjects.map((productObject,index) => (
                <ImageWrapper key={index} src={productObject.image} onClick={(e) => onClick(e)}>
                    
                </ImageWrapper>
            ))}
        </Wrapper>
    )
}

I have an array of data that I'm iterating through, In the javascript map() function call I'm using the second argument that map() takes, 'index'. How would I pass index to the onClick event? I tried adding index to the list of arguments for onClick but all I can access are the events. Is there anyway I can pass the index through?

I'd like to be able to execute the mented out line 'handleImageClick(productObjects[index])'. I need to let the parent ponent know which productObject was clicked on by the user.

export default function SideProducts(props) {
    
const { productObjects, handleImageClick } = props;


const onClick = (e) => {
    e.persist()
    console.log(e)
    //handleImageClick(productObjects[index])
}

console.log(productObjects)
    return (
        <Wrapper>
            {productObjects.map((productObject,index) => (
                <ImageWrapper key={index} src={productObject.image} onClick={(e) => onClick(e)}>
                    
                </ImageWrapper>
            ))}
        </Wrapper>
    )
}
Share Improve this question asked Aug 23, 2021 at 4:49 BenBen 1093 silver badges12 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

I am doing this without testing, but you should be able to just pass the index like this.

const onClick = (e, index) => {
    e.persist()
    console.log(e)
    //handleImageClick(productObjects[index])
}

console.log(productObjects)
    return (
        <Wrapper>
            {productObjects.map((productObject,index) => (
                <ImageWrapper key={index} src={productObject.image} onClick={(e) => onClick(e, index)}>
                    
                </ImageWrapper>
            ))}
        </Wrapper>
    )
}

This way. please go through this blog to learn more about passing arguments with onClick

export default function SideProducts(props) {
    
const { productObjects, handleImageClick } = props;


const onClick = (e, index) => {
    e.persist()
    console.log(e, index)
    //handleImageClick(productObjects[index])
}

console.log(productObjects)
    return (
        <Wrapper>
            {productObjects.map((productObject,index) => (
                <ImageWrapper key={index} src={productObject.image} onClick={(e) => onClick(e, index)}>
                    
                </ImageWrapper>
            ))}
        </Wrapper>
    )
}
  1. First you need to pass the index as argument. onClick={(e) => onClick(e, index)}

  2. Then receive that index as functional parameter.

const onClick = (e, index) => {
    e.persist()
    console.log(e, index)
    //handleImageClick(productObjects[index])
}

Just add it as a parameter to yow onClick

        <ImageWrapper key={index} src={productObject.image} onClick={(e) => onClick(e,productObject.addThePropHere)}>
                    
                </ImageWrapper>

Then in your function just add it as well

const onClick = (e,prop)=> ….code

You can use a higher-order function here to pass params as you want:

const handleWithIndex= index => {
    return (event) => {
         e.persist()
         //handleImageClick(productObjects[index])
    }
}

return (
        <Wrapper>
            {productObjects.map((productObject,index) => (
                <ImageWrapper key={index} src={productObject.image} onClick={handleWithIndex(index)}>
                    
                </ImageWrapper>
            ))}
        </Wrapper>
    )
}
发布评论

评论列表(0)

  1. 暂无评论