# React js onClick property of NavDropdown.Item react- bootstrap

**URL:** <https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613>\
**Category:** General\
**Created:** [April 13, 2020, 6:10pm UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613 "2020-04-13T18:10:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVGY](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dvgy/32/130717_2.png) [@DVGY](https://forum.freecodecamp.org/u/DVGY)\
**Post date:** [April 13, 2020, 6:10pm UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/1 "2020-04-13T18:10:09Z")

</div>

[My code](https://codesandbox.io/s/markdown-previewer-drfbi?file=/src/components/NavBar.jsx)

```auto
<NavDropdown title="Theme" id="basic-nav-dropdown">
          <NavDropdown.Item href="#action/3.1">Dark Mode</NavDropdown.Item>
          <NavDropdown.Item href="#action/3.2">Light Mode</NavDropdown.Item>
          <NavDropdown.Item href="#action/3.3">Green Slate</NavDropdown.Item>
 </NavDropdown>

```

if i put `onClick = {console.log("First button clicked)}` inside `NavDropdown.Item/>` it outputs on console `First button clicked` without even clicking it. ??? Why ?? .

How can i use the `onClick` property to change some `state` ?

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [April 13, 2020, 6:46pm UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/2 "2020-04-13T18:46:48Z")

</div>

Hello there.

This behaviour is the same reason that, when you add a callback function to an element, you pass in the declaration, and not the function call. Example:

```auto
const myComp = () => {
  const [myState, setMyState] = React.useState(0);
  const callbackFunc = () => {
    console.log("Logged");
    setMyState(2);
  }
  return(
    <div>
       <button onClick={callbackFunc}>Click me to log</button>
       <button onClick={callbackFunc()}>I have already invoked the callback</button>
    </div>
  )
}

```

> [@DVGY](#):
>
> How can i use the `onClick` property to change some `state` ?

This is covered in the React section of the [www.freeCodeCamp.org/learn](http://www.freeCodeCamp.org/learn) course. But the above example also includes this…

I hope this helps

---

<div class="post-metadata">

**Author:** ![DVGY](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dvgy/32/130717_2.png) [@DVGY](https://forum.freecodecamp.org/u/DVGY)\
**Post date:** [April 14, 2020, 5:43am UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/3 "2020-04-14T05:43:58Z")

</div>

> [@Sky020](#):
>
> const callbackFunc = () =\> { console.log(“Logged”); setMyState(2); }

Hello  
I did this and **it’s not working pls help**  
i added this piece of code inside App.js

```auto
clickThemeFunc() {
    console.log("button is clicked");
    this.setState({ click: !this.state.click });
    console.log("value of theme: ", this.state.clickTheme);
  }

```

```auto
this.state = {
      textMarkdown: "This is inital state text",
      clickTheme: false
    };

```

Inside Navbar.js i added

```auto
 <NavDropdown.Item onClick={props.clickThemeFunc} href="#action/3.1">
            Dark Mode
          </NavDropdown.Item>

```

**still i can’t any seething on `console` why ??**

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [April 14, 2020, 11:53am UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/4 "2020-04-14T11:53:31Z")

</div>

I highly recommend you review the React section of the curriculum.

```auto
this.setState({ click: !this.state.click });

```

You cannot set `click` as a state using its value, when you have not declared it - `this.state.click` does not exist.

Also, you have two methods in a class component, but only one of them you have bound with `this`.

```javascript
clickThemeFunc={this.clickThemeFunc}
// You have not set 'this' to be related to the function handle

```

---

<div class="post-metadata">

**Author:** ![DVGY](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dvgy/32/130717_2.png) [@DVGY](https://forum.freecodecamp.org/u/DVGY)\
**Post date:** [April 14, 2020, 12:45pm UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/5 "2020-04-14T12:45:22Z")

</div>

Ok so it was a typing mistake sorry for that.

```auto
class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      textMarkdown: defaultMarkup,
      clickTheme: false
    };

    this.handleTextChange = this.handleTextChange.bind(this);
    this.clickThemeFunc = this.clickThemeFunc.bind(this);
  }
  clickThemeFunc() {
    console.log("button is clicked");
    this.setState({ clickTheme: !this.state.clickTheme });
    console.log("value of theme: ", this.state.clickTheme);
  }

```

Can you tell why is it still not working ? I cannot see anything on console.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [April 14, 2020, 2:36pm UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/6 "2020-04-14T14:36:25Z")

</div>

I cannot see why the `onClick` is not firing. I have never used React-Bootstrap, but I assume the issue is there.

Sorry, I could not help any further.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:58pm UTC](https://forum.freecodecamp.org/t/react-js-onclick-property-of-navdropdown-item-react-bootstrap/372613/7 "2021-06-01T18:58:39Z")

</div>

Closed on June 1, 2021, 6:58pm UTC


