React How to fire onClick each from map function












0














I have some lists which are mapped and I'd like to fire onClick when I click an element(which is one of objects from the array).



Let's say there are 3 lists and when I click one of them, I'd like to change "the element that i clicked"'s class to 'open' from 'close'.



state= {
open: false
}

handleClick = () => {
this.setState({
open: !this.state.open
})
}

Array.map((list, index) => {
<div key={index} onClick={this.handleClick}>
<p className={this.state.open? 'open' : 'close'}>{list.title}</p>
</div>
})


.



Array = [{
title: 'a',
link: '/service/wallet'
},{
title: 'b',
link: '/service/home'
}]


I have a value of this.props.location.pathname and I think I can compare this to Array[i].link.



something like this?



if(this.props.location.pathname === Array[0].link){

}


However, I don't know how to complete the code for this issue. please tell if my idea is right and some hints.










share|improve this question




















  • 1




    onClick={() => this.handleClick(list)} - pass whatever you want to handleClick
    – vsync
    Nov 12 at 12:31


















0














I have some lists which are mapped and I'd like to fire onClick when I click an element(which is one of objects from the array).



Let's say there are 3 lists and when I click one of them, I'd like to change "the element that i clicked"'s class to 'open' from 'close'.



state= {
open: false
}

handleClick = () => {
this.setState({
open: !this.state.open
})
}

Array.map((list, index) => {
<div key={index} onClick={this.handleClick}>
<p className={this.state.open? 'open' : 'close'}>{list.title}</p>
</div>
})


.



Array = [{
title: 'a',
link: '/service/wallet'
},{
title: 'b',
link: '/service/home'
}]


I have a value of this.props.location.pathname and I think I can compare this to Array[i].link.



something like this?



if(this.props.location.pathname === Array[0].link){

}


However, I don't know how to complete the code for this issue. please tell if my idea is right and some hints.










share|improve this question




















  • 1




    onClick={() => this.handleClick(list)} - pass whatever you want to handleClick
    – vsync
    Nov 12 at 12:31
















0












0








0







I have some lists which are mapped and I'd like to fire onClick when I click an element(which is one of objects from the array).



Let's say there are 3 lists and when I click one of them, I'd like to change "the element that i clicked"'s class to 'open' from 'close'.



state= {
open: false
}

handleClick = () => {
this.setState({
open: !this.state.open
})
}

Array.map((list, index) => {
<div key={index} onClick={this.handleClick}>
<p className={this.state.open? 'open' : 'close'}>{list.title}</p>
</div>
})


.



Array = [{
title: 'a',
link: '/service/wallet'
},{
title: 'b',
link: '/service/home'
}]


I have a value of this.props.location.pathname and I think I can compare this to Array[i].link.



something like this?



if(this.props.location.pathname === Array[0].link){

}


However, I don't know how to complete the code for this issue. please tell if my idea is right and some hints.










share|improve this question















I have some lists which are mapped and I'd like to fire onClick when I click an element(which is one of objects from the array).



Let's say there are 3 lists and when I click one of them, I'd like to change "the element that i clicked"'s class to 'open' from 'close'.



state= {
open: false
}

handleClick = () => {
this.setState({
open: !this.state.open
})
}

Array.map((list, index) => {
<div key={index} onClick={this.handleClick}>
<p className={this.state.open? 'open' : 'close'}>{list.title}</p>
</div>
})


.



Array = [{
title: 'a',
link: '/service/wallet'
},{
title: 'b',
link: '/service/home'
}]


I have a value of this.props.location.pathname and I think I can compare this to Array[i].link.



something like this?



if(this.props.location.pathname === Array[0].link){

}


However, I don't know how to complete the code for this issue. please tell if my idea is right and some hints.







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:30









vsync

45.3k35157217




45.3k35157217










asked Nov 12 at 12:25









Mijeong Won

446




446








  • 1




    onClick={() => this.handleClick(list)} - pass whatever you want to handleClick
    – vsync
    Nov 12 at 12:31
















  • 1




    onClick={() => this.handleClick(list)} - pass whatever you want to handleClick
    – vsync
    Nov 12 at 12:31










1




1




onClick={() => this.handleClick(list)} - pass whatever you want to handleClick
– vsync
Nov 12 at 12:31






onClick={() => this.handleClick(list)} - pass whatever you want to handleClick
– vsync
Nov 12 at 12:31














2 Answers
2






active

oldest

votes


















2














You'll need to keep the "is it clicked?" information in this.state. Because it's a list of things you need to keep track of you can't store the state in a single variable, you'll need a map of them.



state= {
open: {}
}

handleClick = (link) => {
let linkOpenState = false;
if (this.state.open.hasOwnProperty(link)) {
linkOpenState = this.state.open[link];
}

this.setState({ open: { [link]: linkOpenState } })
}

Array.map((list, index) => {
<div key={index} onClick={this.handleClick.bind(this, list.link)}>
<p className={this.state.open[list.link]? 'open' : 'close'}>{list.title}</p>
</div>
})


You need to get used to thinking "the React way". State needs to be kept in component state (or if complexity becomes a problem then look at Redux). You don't imperatively manipulate the DOM and you don't "ask" the DOM for information, you tell it how it should look based on the state.






share|improve this answer



















  • 1




    Indeed. I updated my answer.
    – Sergiu Paraschiv
    Nov 12 at 12:35





















0














Please have a look at this code mapping data with react elements



To solve this problem, you can have a state which tracks the interaction for a group of elements, in your case it is list of elements.
here you can use a map in a state variable and have {key,value} pair associated with each element to track it.
Hope this helps.
Thanks.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262169%2freact-how-to-fire-onclick-each-from-map-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You'll need to keep the "is it clicked?" information in this.state. Because it's a list of things you need to keep track of you can't store the state in a single variable, you'll need a map of them.



    state= {
    open: {}
    }

    handleClick = (link) => {
    let linkOpenState = false;
    if (this.state.open.hasOwnProperty(link)) {
    linkOpenState = this.state.open[link];
    }

    this.setState({ open: { [link]: linkOpenState } })
    }

    Array.map((list, index) => {
    <div key={index} onClick={this.handleClick.bind(this, list.link)}>
    <p className={this.state.open[list.link]? 'open' : 'close'}>{list.title}</p>
    </div>
    })


    You need to get used to thinking "the React way". State needs to be kept in component state (or if complexity becomes a problem then look at Redux). You don't imperatively manipulate the DOM and you don't "ask" the DOM for information, you tell it how it should look based on the state.






    share|improve this answer



















    • 1




      Indeed. I updated my answer.
      – Sergiu Paraschiv
      Nov 12 at 12:35


















    2














    You'll need to keep the "is it clicked?" information in this.state. Because it's a list of things you need to keep track of you can't store the state in a single variable, you'll need a map of them.



    state= {
    open: {}
    }

    handleClick = (link) => {
    let linkOpenState = false;
    if (this.state.open.hasOwnProperty(link)) {
    linkOpenState = this.state.open[link];
    }

    this.setState({ open: { [link]: linkOpenState } })
    }

    Array.map((list, index) => {
    <div key={index} onClick={this.handleClick.bind(this, list.link)}>
    <p className={this.state.open[list.link]? 'open' : 'close'}>{list.title}</p>
    </div>
    })


    You need to get used to thinking "the React way". State needs to be kept in component state (or if complexity becomes a problem then look at Redux). You don't imperatively manipulate the DOM and you don't "ask" the DOM for information, you tell it how it should look based on the state.






    share|improve this answer



















    • 1




      Indeed. I updated my answer.
      – Sergiu Paraschiv
      Nov 12 at 12:35
















    2












    2








    2






    You'll need to keep the "is it clicked?" information in this.state. Because it's a list of things you need to keep track of you can't store the state in a single variable, you'll need a map of them.



    state= {
    open: {}
    }

    handleClick = (link) => {
    let linkOpenState = false;
    if (this.state.open.hasOwnProperty(link)) {
    linkOpenState = this.state.open[link];
    }

    this.setState({ open: { [link]: linkOpenState } })
    }

    Array.map((list, index) => {
    <div key={index} onClick={this.handleClick.bind(this, list.link)}>
    <p className={this.state.open[list.link]? 'open' : 'close'}>{list.title}</p>
    </div>
    })


    You need to get used to thinking "the React way". State needs to be kept in component state (or if complexity becomes a problem then look at Redux). You don't imperatively manipulate the DOM and you don't "ask" the DOM for information, you tell it how it should look based on the state.






    share|improve this answer














    You'll need to keep the "is it clicked?" information in this.state. Because it's a list of things you need to keep track of you can't store the state in a single variable, you'll need a map of them.



    state= {
    open: {}
    }

    handleClick = (link) => {
    let linkOpenState = false;
    if (this.state.open.hasOwnProperty(link)) {
    linkOpenState = this.state.open[link];
    }

    this.setState({ open: { [link]: linkOpenState } })
    }

    Array.map((list, index) => {
    <div key={index} onClick={this.handleClick.bind(this, list.link)}>
    <p className={this.state.open[list.link]? 'open' : 'close'}>{list.title}</p>
    </div>
    })


    You need to get used to thinking "the React way". State needs to be kept in component state (or if complexity becomes a problem then look at Redux). You don't imperatively manipulate the DOM and you don't "ask" the DOM for information, you tell it how it should look based on the state.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 12:36

























    answered Nov 12 at 12:30









    Sergiu Paraschiv

    8,52432741




    8,52432741








    • 1




      Indeed. I updated my answer.
      – Sergiu Paraschiv
      Nov 12 at 12:35
















    • 1




      Indeed. I updated my answer.
      – Sergiu Paraschiv
      Nov 12 at 12:35










    1




    1




    Indeed. I updated my answer.
    – Sergiu Paraschiv
    Nov 12 at 12:35






    Indeed. I updated my answer.
    – Sergiu Paraschiv
    Nov 12 at 12:35















    0














    Please have a look at this code mapping data with react elements



    To solve this problem, you can have a state which tracks the interaction for a group of elements, in your case it is list of elements.
    here you can use a map in a state variable and have {key,value} pair associated with each element to track it.
    Hope this helps.
    Thanks.






    share|improve this answer


























      0














      Please have a look at this code mapping data with react elements



      To solve this problem, you can have a state which tracks the interaction for a group of elements, in your case it is list of elements.
      here you can use a map in a state variable and have {key,value} pair associated with each element to track it.
      Hope this helps.
      Thanks.






      share|improve this answer
























        0












        0








        0






        Please have a look at this code mapping data with react elements



        To solve this problem, you can have a state which tracks the interaction for a group of elements, in your case it is list of elements.
        here you can use a map in a state variable and have {key,value} pair associated with each element to track it.
        Hope this helps.
        Thanks.






        share|improve this answer












        Please have a look at this code mapping data with react elements



        To solve this problem, you can have a state which tracks the interaction for a group of elements, in your case it is list of elements.
        here you can use a map in a state variable and have {key,value} pair associated with each element to track it.
        Hope this helps.
        Thanks.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 13:16









        Shubham

        14115




        14115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262169%2freact-how-to-fire-onclick-each-from-map-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly