Button with onClick only works once in React





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I need to make a collapse menu in a React app, but the button with onClick only works once. I created a boolean variable and it should change when I tap the button. But I can tap the button only once and thereafter the <a> doesn't work, it's inactive:



let isOpened = false;

class Header extends React.Component {
handleClick = () => {
isOpened = !isOpened;
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>


And the collapse code:



<Collapse isOpened={isOpened}>
<nav className={`${s.menu} ${s.mobile}`}>
<ul className={s['menu-ul']}>
...
</ul>
</nav>
</Collapse>









share|improve this question

























  • The render function in the code snippet Header class - and the whole class - is incomplete. I guess you omitted the end of it by accident (?).

    – ProgrammerPer
    Nov 25 '18 at 9:08













  • @ProgrammerPer In my code render function complete. I put away some parts that code would be compactly

    – Temniy
    Nov 25 '18 at 9:56


















1















I need to make a collapse menu in a React app, but the button with onClick only works once. I created a boolean variable and it should change when I tap the button. But I can tap the button only once and thereafter the <a> doesn't work, it's inactive:



let isOpened = false;

class Header extends React.Component {
handleClick = () => {
isOpened = !isOpened;
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>


And the collapse code:



<Collapse isOpened={isOpened}>
<nav className={`${s.menu} ${s.mobile}`}>
<ul className={s['menu-ul']}>
...
</ul>
</nav>
</Collapse>









share|improve this question

























  • The render function in the code snippet Header class - and the whole class - is incomplete. I guess you omitted the end of it by accident (?).

    – ProgrammerPer
    Nov 25 '18 at 9:08













  • @ProgrammerPer In my code render function complete. I put away some parts that code would be compactly

    – Temniy
    Nov 25 '18 at 9:56














1












1








1








I need to make a collapse menu in a React app, but the button with onClick only works once. I created a boolean variable and it should change when I tap the button. But I can tap the button only once and thereafter the <a> doesn't work, it's inactive:



let isOpened = false;

class Header extends React.Component {
handleClick = () => {
isOpened = !isOpened;
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>


And the collapse code:



<Collapse isOpened={isOpened}>
<nav className={`${s.menu} ${s.mobile}`}>
<ul className={s['menu-ul']}>
...
</ul>
</nav>
</Collapse>









share|improve this question
















I need to make a collapse menu in a React app, but the button with onClick only works once. I created a boolean variable and it should change when I tap the button. But I can tap the button only once and thereafter the <a> doesn't work, it's inactive:



let isOpened = false;

class Header extends React.Component {
handleClick = () => {
isOpened = !isOpened;
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>


And the collapse code:



<Collapse isOpened={isOpened}>
<nav className={`${s.menu} ${s.mobile}`}>
<ul className={s['menu-ul']}>
...
</ul>
</nav>
</Collapse>






javascript reactjs jsx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 10:09









ProgrammerPer

795814




795814










asked Nov 25 '18 at 8:53









TemniyTemniy

106




106













  • The render function in the code snippet Header class - and the whole class - is incomplete. I guess you omitted the end of it by accident (?).

    – ProgrammerPer
    Nov 25 '18 at 9:08













  • @ProgrammerPer In my code render function complete. I put away some parts that code would be compactly

    – Temniy
    Nov 25 '18 at 9:56



















  • The render function in the code snippet Header class - and the whole class - is incomplete. I guess you omitted the end of it by accident (?).

    – ProgrammerPer
    Nov 25 '18 at 9:08













  • @ProgrammerPer In my code render function complete. I put away some parts that code would be compactly

    – Temniy
    Nov 25 '18 at 9:56

















The render function in the code snippet Header class - and the whole class - is incomplete. I guess you omitted the end of it by accident (?).

– ProgrammerPer
Nov 25 '18 at 9:08







The render function in the code snippet Header class - and the whole class - is incomplete. I guess you omitted the end of it by accident (?).

– ProgrammerPer
Nov 25 '18 at 9:08















@ProgrammerPer In my code render function complete. I put away some parts that code would be compactly

– Temniy
Nov 25 '18 at 9:56





@ProgrammerPer In my code render function complete. I put away some parts that code would be compactly

– Temniy
Nov 25 '18 at 9:56












2 Answers
2






active

oldest

votes


















3














Your onClick is working fine but it is not re-rendering your react component. You need to put isOpened in the state of the component and global variable. Please read this



class Header extends React.Component {
constructor(props){
super(props);

this.state = {isOpened: false};
}
handleClick = () => {
this.setState({isOpened: !this.state.isOpened});
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>
);
}





share|improve this answer


























  • I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

    – Temniy
    Nov 25 '18 at 9:09













  • Updated the answer

    – Ajay Gaur
    Nov 25 '18 at 9:12











  • Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

    – Temniy
    Nov 25 '18 at 9:21



















0














Addendum to answer of @Ajay Gaur



Your problem is in the using <a href="#" onClick={this.handleClick}>
When you try to click on a link browser want to redirect you to the next page becouse in a you added href="#".
So there are two ways to solve your problem.



1: Use button instead a tag.



2: Add preventDefaul to your method:



handleClick = (e) => {
e.preventDefault();
this.setState({ isOpened: !this.state.isOpened });
};


It should to help.






share|improve this answer


























  • I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

    – Temniy
    Nov 25 '18 at 14:41












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%2f53465983%2fbutton-with-onclick-only-works-once-in-react%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









3














Your onClick is working fine but it is not re-rendering your react component. You need to put isOpened in the state of the component and global variable. Please read this



class Header extends React.Component {
constructor(props){
super(props);

this.state = {isOpened: false};
}
handleClick = () => {
this.setState({isOpened: !this.state.isOpened});
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>
);
}





share|improve this answer


























  • I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

    – Temniy
    Nov 25 '18 at 9:09













  • Updated the answer

    – Ajay Gaur
    Nov 25 '18 at 9:12











  • Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

    – Temniy
    Nov 25 '18 at 9:21
















3














Your onClick is working fine but it is not re-rendering your react component. You need to put isOpened in the state of the component and global variable. Please read this



class Header extends React.Component {
constructor(props){
super(props);

this.state = {isOpened: false};
}
handleClick = () => {
this.setState({isOpened: !this.state.isOpened});
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>
);
}





share|improve this answer


























  • I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

    – Temniy
    Nov 25 '18 at 9:09













  • Updated the answer

    – Ajay Gaur
    Nov 25 '18 at 9:12











  • Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

    – Temniy
    Nov 25 '18 at 9:21














3












3








3







Your onClick is working fine but it is not re-rendering your react component. You need to put isOpened in the state of the component and global variable. Please read this



class Header extends React.Component {
constructor(props){
super(props);

this.state = {isOpened: false};
}
handleClick = () => {
this.setState({isOpened: !this.state.isOpened});
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>
);
}





share|improve this answer















Your onClick is working fine but it is not re-rendering your react component. You need to put isOpened in the state of the component and global variable. Please read this



class Header extends React.Component {
constructor(props){
super(props);

this.state = {isOpened: false};
}
handleClick = () => {
this.setState({isOpened: !this.state.isOpened});
};

render() {
const path = history.location && history.location.pathname;
return (
<div className={s['header-left']}>
<div className={s.button}>
<a href="#" onClick={this.handleClick}>
<FontAwesomeIcon icon={faBars} />
</a>
</div>
);
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 9:12

























answered Nov 25 '18 at 8:57









Ajay GaurAjay Gaur

2,23731935




2,23731935













  • I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

    – Temniy
    Nov 25 '18 at 9:09













  • Updated the answer

    – Ajay Gaur
    Nov 25 '18 at 9:12











  • Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

    – Temniy
    Nov 25 '18 at 9:21



















  • I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

    – Temniy
    Nov 25 '18 at 9:09













  • Updated the answer

    – Ajay Gaur
    Nov 25 '18 at 9:12











  • Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

    – Temniy
    Nov 25 '18 at 9:21

















I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

– Temniy
Nov 25 '18 at 9:09







I have an error ReferenceError: isOpened is not defined Header/< C:/Users/Роман/WebstormProjects/pbh_web-master/src/components/Header/Header.js:20 17 | this.state = {isOpened: false}; 18 | } 19 | handleClick = () => { > 20 | this.setState({isOpened: !isOpened}); | ^ 21 | }; 22 | 23 | render() {

– Temniy
Nov 25 '18 at 9:09















Updated the answer

– Ajay Gaur
Nov 25 '18 at 9:12





Updated the answer

– Ajay Gaur
Nov 25 '18 at 9:12













Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

– Temniy
Nov 25 '18 at 9:21





Hm, after all fixes button work only once time, when I'm on start page, cursor highlights my button as a link (it's work). But when I tab on button, it stand unactive, cursor highlights button as text or picture and when I trying to tab on it, nothing happens

– Temniy
Nov 25 '18 at 9:21













0














Addendum to answer of @Ajay Gaur



Your problem is in the using <a href="#" onClick={this.handleClick}>
When you try to click on a link browser want to redirect you to the next page becouse in a you added href="#".
So there are two ways to solve your problem.



1: Use button instead a tag.



2: Add preventDefaul to your method:



handleClick = (e) => {
e.preventDefault();
this.setState({ isOpened: !this.state.isOpened });
};


It should to help.






share|improve this answer


























  • I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

    – Temniy
    Nov 25 '18 at 14:41
















0














Addendum to answer of @Ajay Gaur



Your problem is in the using <a href="#" onClick={this.handleClick}>
When you try to click on a link browser want to redirect you to the next page becouse in a you added href="#".
So there are two ways to solve your problem.



1: Use button instead a tag.



2: Add preventDefaul to your method:



handleClick = (e) => {
e.preventDefault();
this.setState({ isOpened: !this.state.isOpened });
};


It should to help.






share|improve this answer


























  • I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

    – Temniy
    Nov 25 '18 at 14:41














0












0








0







Addendum to answer of @Ajay Gaur



Your problem is in the using <a href="#" onClick={this.handleClick}>
When you try to click on a link browser want to redirect you to the next page becouse in a you added href="#".
So there are two ways to solve your problem.



1: Use button instead a tag.



2: Add preventDefaul to your method:



handleClick = (e) => {
e.preventDefault();
this.setState({ isOpened: !this.state.isOpened });
};


It should to help.






share|improve this answer















Addendum to answer of @Ajay Gaur



Your problem is in the using <a href="#" onClick={this.handleClick}>
When you try to click on a link browser want to redirect you to the next page becouse in a you added href="#".
So there are two ways to solve your problem.



1: Use button instead a tag.



2: Add preventDefaul to your method:



handleClick = (e) => {
e.preventDefault();
this.setState({ isOpened: !this.state.isOpened });
};


It should to help.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 10:59

























answered Nov 25 '18 at 10:53









eclipseeereclipseeer

233




233













  • I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

    – Temniy
    Nov 25 '18 at 14:41



















  • I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

    – Temniy
    Nov 25 '18 at 14:41

















I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

– Temniy
Nov 25 '18 at 14:41





I solve the problem. The previous answer was right. It will be problems wuth markup, the menu was over button

– Temniy
Nov 25 '18 at 14:41


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465983%2fbutton-with-onclick-only-works-once-in-react%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()