How to add active class to a list of buttons onClick, they are not controlled by parentReact Js conditionally applying class attributesreact change the class of list item on clickSet focus on a react-bootstrap.Input field using refs.x.getDOMNode.focusHow to conditionally add attributes to React components?Passing data to parent in ReactHow do I reset class names on all JSX elements in a component on props received?setState changes state but doesn't re-renderupdate state child from parent's onClick on reactjsCan I connect just the onClick function to the Redux store, instead of connecting the button component?empty property and value in React fetch post requestReact Ref not getting text input field data in formReact Checkbox Depends on Parent State

What prevents ads from reading my password as I type it?

Did WWII Japanese soldiers engage in cannibalism of their enemies?

Pronouns when writing from the point of view of a robot

What does the ISO setting for mechanical 35mm film cameras actually do?

Repeated! Factorials!

Getting matrices labels

I was contacted by a private bank overseas to get my inheritance

Is space radiation a risk for space film photography, and how is this prevented?

Why am I not getting stuck in the loop

If the interviewer says "We have other interviews to conduct and then back to you in few days", is it a bad sign to not get the job?

What could prevent players from leaving an island?

What is the German idiom or expression for when someone is being hypocritical against their own teachings?

Did Captain America make out with his niece?

How to approach protecting my code as a research assistant? Should I be worried in the first place?

Changing Row Keys into Normal Rows

Is it double speak?

Is there a way to upload multiple discount counts into CiviDiscount?

Do you like Music? This word is Historic

Delete a folder with a lot of files

Why do proponents of guns oppose gun competency tests?

Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?

Non-small objects in categories

Did Apollo leave poop on the moon?

Does this smartphone photo show Mars just below the Sun?



How to add active class to a list of buttons onClick, they are not controlled by parent


React Js conditionally applying class attributesreact change the class of list item on clickSet focus on a react-bootstrap.Input field using refs.x.getDOMNode.focusHow to conditionally add attributes to React components?Passing data to parent in ReactHow do I reset class names on all JSX elements in a component on props received?setState changes state but doesn't re-renderupdate state child from parent's onClick on reactjsCan I connect just the onClick function to the Redux store, instead of connecting the button component?empty property and value in React fetch post requestReact Ref not getting text input field data in formReact Checkbox Depends on Parent State






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".



My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?



I have tried these solutions but these all are parent based.
React Js conditionally applying class attributes
react change the class of list item on click



import React from "react"
import StyleButton from "./StyleButton"

class Post extends React.Component
constructor()
super()
this.state =
selectedClass: 'red',
displayBtns: true,
buttons: [1,2,3,4,5,6]



handleDisplayBtnClick = () =>
this.setState(prevState => (
displayBtns: !prevState.displayBtns
))


handleChange = e =>
let btnId = e.currentTarget.dataset.id;
switch (btnId)
case '1':
this.setState(selectedClass: "purple" )
break;
case '2':
this.setState(selectedClass: "cyan" )
break;
case '3':
this.setState(selectedClass: "ballons" )
break;
case '4':
this.setState(selectedClass: "clouds" )
break;
case '5':
this.setState(selectedClass: "thumbs" )
break;
case '6':
this.setState(selectedClass: "smile" )
break;
default:
this.setState(selectedClass: "default" )



render()
return(
<div className=`bg container $this.state.selectedClass` >
<input className="input" placeholder="What's on your mind?" type="text"/>
<button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick>&nbsp;</button>

this.state.displayBtns ?
<div className="btns" >
this.state.buttons.map(eachButtonNumber =>
<button
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange
>&nbsp;</button>
)
</div>
: null
</div>
)



export default Post


I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.



<button 
key=eachButtonNumber
data-id=eachButtonNumber
onClick=this.handleChange >&nbsp;</button>


Also, want to know If this is the right way of doing it?



Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"



My Rendered HTML - I want "active" on the list of buttons.



<div class="parent">
<div class="bg container smile">
<input class="input" placeholder="What's on your mind?" type="text">
<button class="closeBtn">&nbsp;</button>
<div class="btns">
<button data-id="1">&nbsp;</button>
<button data-id="2">&nbsp;</button>
<button data-id="3">&nbsp;</button>
<button data-id="4">&nbsp;</button>
<button data-id="5">&nbsp;</button>
<button data-id="6">&nbsp;</button>
</div>
</div>
</div>









share|improve this question
































    0















    I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".



    My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?



    I have tried these solutions but these all are parent based.
    React Js conditionally applying class attributes
    react change the class of list item on click



    import React from "react"
    import StyleButton from "./StyleButton"

    class Post extends React.Component
    constructor()
    super()
    this.state =
    selectedClass: 'red',
    displayBtns: true,
    buttons: [1,2,3,4,5,6]



    handleDisplayBtnClick = () =>
    this.setState(prevState => (
    displayBtns: !prevState.displayBtns
    ))


    handleChange = e =>
    let btnId = e.currentTarget.dataset.id;
    switch (btnId)
    case '1':
    this.setState(selectedClass: "purple" )
    break;
    case '2':
    this.setState(selectedClass: "cyan" )
    break;
    case '3':
    this.setState(selectedClass: "ballons" )
    break;
    case '4':
    this.setState(selectedClass: "clouds" )
    break;
    case '5':
    this.setState(selectedClass: "thumbs" )
    break;
    case '6':
    this.setState(selectedClass: "smile" )
    break;
    default:
    this.setState(selectedClass: "default" )



    render()
    return(
    <div className=`bg container $this.state.selectedClass` >
    <input className="input" placeholder="What's on your mind?" type="text"/>
    <button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick>&nbsp;</button>

    this.state.displayBtns ?
    <div className="btns" >
    this.state.buttons.map(eachButtonNumber =>
    <button
    key=eachButtonNumber
    data-id=eachButtonNumber
    onClick=this.handleChange
    >&nbsp;</button>
    )
    </div>
    : null
    </div>
    )



    export default Post


    I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.



    <button 
    key=eachButtonNumber
    data-id=eachButtonNumber
    onClick=this.handleChange >&nbsp;</button>


    Also, want to know If this is the right way of doing it?



    Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"



    My Rendered HTML - I want "active" on the list of buttons.



    <div class="parent">
    <div class="bg container smile">
    <input class="input" placeholder="What's on your mind?" type="text">
    <button class="closeBtn">&nbsp;</button>
    <div class="btns">
    <button data-id="1">&nbsp;</button>
    <button data-id="2">&nbsp;</button>
    <button data-id="3">&nbsp;</button>
    <button data-id="4">&nbsp;</button>
    <button data-id="5">&nbsp;</button>
    <button data-id="6">&nbsp;</button>
    </div>
    </div>
    </div>









    share|improve this question




























      0












      0








      0








      I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".



      My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?



      I have tried these solutions but these all are parent based.
      React Js conditionally applying class attributes
      react change the class of list item on click



      import React from "react"
      import StyleButton from "./StyleButton"

      class Post extends React.Component
      constructor()
      super()
      this.state =
      selectedClass: 'red',
      displayBtns: true,
      buttons: [1,2,3,4,5,6]



      handleDisplayBtnClick = () =>
      this.setState(prevState => (
      displayBtns: !prevState.displayBtns
      ))


      handleChange = e =>
      let btnId = e.currentTarget.dataset.id;
      switch (btnId)
      case '1':
      this.setState(selectedClass: "purple" )
      break;
      case '2':
      this.setState(selectedClass: "cyan" )
      break;
      case '3':
      this.setState(selectedClass: "ballons" )
      break;
      case '4':
      this.setState(selectedClass: "clouds" )
      break;
      case '5':
      this.setState(selectedClass: "thumbs" )
      break;
      case '6':
      this.setState(selectedClass: "smile" )
      break;
      default:
      this.setState(selectedClass: "default" )



      render()
      return(
      <div className=`bg container $this.state.selectedClass` >
      <input className="input" placeholder="What's on your mind?" type="text"/>
      <button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick>&nbsp;</button>

      this.state.displayBtns ?
      <div className="btns" >
      this.state.buttons.map(eachButtonNumber =>
      <button
      key=eachButtonNumber
      data-id=eachButtonNumber
      onClick=this.handleChange
      >&nbsp;</button>
      )
      </div>
      : null
      </div>
      )



      export default Post


      I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.



      <button 
      key=eachButtonNumber
      data-id=eachButtonNumber
      onClick=this.handleChange >&nbsp;</button>


      Also, want to know If this is the right way of doing it?



      Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"



      My Rendered HTML - I want "active" on the list of buttons.



      <div class="parent">
      <div class="bg container smile">
      <input class="input" placeholder="What's on your mind?" type="text">
      <button class="closeBtn">&nbsp;</button>
      <div class="btns">
      <button data-id="1">&nbsp;</button>
      <button data-id="2">&nbsp;</button>
      <button data-id="3">&nbsp;</button>
      <button data-id="4">&nbsp;</button>
      <button data-id="5">&nbsp;</button>
      <button data-id="6">&nbsp;</button>
      </div>
      </div>
      </div>









      share|improve this question
















      I am trying to create a chatbox component like the facebook-status post. There are buttons on the bottom. When a user clicks them I load various box styles. I need to set the class of current button "active".



      My component is not controlled by a parent so I cannot set any prop bases solution. I know how to implement a ternary operator but don't know how can I use it here?



      I have tried these solutions but these all are parent based.
      React Js conditionally applying class attributes
      react change the class of list item on click



      import React from "react"
      import StyleButton from "./StyleButton"

      class Post extends React.Component
      constructor()
      super()
      this.state =
      selectedClass: 'red',
      displayBtns: true,
      buttons: [1,2,3,4,5,6]



      handleDisplayBtnClick = () =>
      this.setState(prevState => (
      displayBtns: !prevState.displayBtns
      ))


      handleChange = e =>
      let btnId = e.currentTarget.dataset.id;
      switch (btnId)
      case '1':
      this.setState(selectedClass: "purple" )
      break;
      case '2':
      this.setState(selectedClass: "cyan" )
      break;
      case '3':
      this.setState(selectedClass: "ballons" )
      break;
      case '4':
      this.setState(selectedClass: "clouds" )
      break;
      case '5':
      this.setState(selectedClass: "thumbs" )
      break;
      case '6':
      this.setState(selectedClass: "smile" )
      break;
      default:
      this.setState(selectedClass: "default" )



      render()
      return(
      <div className=`bg container $this.state.selectedClass` >
      <input className="input" placeholder="What's on your mind?" type="text"/>
      <button className=this.state.displayBtns ? 'closeBtn' : 'openBtn' onClick=this.handleDisplayBtnClick>&nbsp;</button>

      this.state.displayBtns ?
      <div className="btns" >
      this.state.buttons.map(eachButtonNumber =>
      <button
      key=eachButtonNumber
      data-id=eachButtonNumber
      onClick=this.handleChange
      >&nbsp;</button>
      )
      </div>
      : null
      </div>
      )



      export default Post


      I want to add an "active" class when I click it on the buttons which are rendered from "buttons.map". Right now my buttons is changing the Div class to change the background.



      <button 
      key=eachButtonNumber
      data-id=eachButtonNumber
      onClick=this.handleChange >&nbsp;</button>


      Also, want to know If this is the right way of doing it?



      Posted the whole code and GIF at "https://github.com/ankursehdev/facebook-post-box-reactjs"



      My Rendered HTML - I want "active" on the list of buttons.



      <div class="parent">
      <div class="bg container smile">
      <input class="input" placeholder="What's on your mind?" type="text">
      <button class="closeBtn">&nbsp;</button>
      <div class="btns">
      <button data-id="1">&nbsp;</button>
      <button data-id="2">&nbsp;</button>
      <button data-id="3">&nbsp;</button>
      <button data-id="4">&nbsp;</button>
      <button data-id="5">&nbsp;</button>
      <button data-id="6">&nbsp;</button>
      </div>
      </div>
      </div>






      reactjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 4:31







      Ankur Sehdev

















      asked Mar 27 at 4:08









      Ankur SehdevAnkur Sehdev

      411 silver badge5 bronze badges




      411 silver badge5 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          1














          If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6] for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:



          [
          id: 1, buttonName: 'purple',
          id: 2, buttonName: 'cyan',
          id: 3, buttonName: 'ballons',
          id: 4, buttonName: 'clouds',
          id: 5, buttonName: 'thumbs',
          id: 6, buttonName: 'smile',
          ]


          Then in your map loop, each button is checking if the selectedClass state matches its buttonName property and adding an active class in that case. Note you will need to point to id property now for the numeric id of each button.



          this.state.buttons.map(eachButtonNumber => 
          <button
          className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
          key=eachButtonNumber.id
          data-id=eachButtonNumber.id
          onClick=this.handleChange
          >
          &nbsp;
          </button>
          )





          share|improve this answer

























          • Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

            – Ankur Sehdev
            Mar 27 at 14:09












          • It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

            – BugsArePeopleToo
            Mar 27 at 14:13











          • I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

            – Ankur Sehdev
            Mar 27 at 14:21



















          0














          Simplest way to do this is to get the className to be applied inside the render before returning the jsx.



          Like in render you can write the condition as below



          let classNameStyle = "" 
          if(this.state.displayBtns)
          classNameStyle = 'closeBtn '
          else
          classNameStyle = 'openBtn '

          if(this.state.active)
          classNameStyle = classNameStyle + 'active'


          Now apply this classNameStyle value to className property of button.
          (Note ) See the space in assignment of first className






          share|improve this answer

























          • Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

            – Ankur Sehdev
            Mar 27 at 4:26














          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%2f55369669%2fhow-to-add-active-class-to-a-list-of-buttons-onclick-they-are-not-controlled-by%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









          1














          If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6] for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:



          [
          id: 1, buttonName: 'purple',
          id: 2, buttonName: 'cyan',
          id: 3, buttonName: 'ballons',
          id: 4, buttonName: 'clouds',
          id: 5, buttonName: 'thumbs',
          id: 6, buttonName: 'smile',
          ]


          Then in your map loop, each button is checking if the selectedClass state matches its buttonName property and adding an active class in that case. Note you will need to point to id property now for the numeric id of each button.



          this.state.buttons.map(eachButtonNumber => 
          <button
          className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
          key=eachButtonNumber.id
          data-id=eachButtonNumber.id
          onClick=this.handleChange
          >
          &nbsp;
          </button>
          )





          share|improve this answer

























          • Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

            – Ankur Sehdev
            Mar 27 at 14:09












          • It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

            – BugsArePeopleToo
            Mar 27 at 14:13











          • I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

            – Ankur Sehdev
            Mar 27 at 14:21
















          1














          If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6] for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:



          [
          id: 1, buttonName: 'purple',
          id: 2, buttonName: 'cyan',
          id: 3, buttonName: 'ballons',
          id: 4, buttonName: 'clouds',
          id: 5, buttonName: 'thumbs',
          id: 6, buttonName: 'smile',
          ]


          Then in your map loop, each button is checking if the selectedClass state matches its buttonName property and adding an active class in that case. Note you will need to point to id property now for the numeric id of each button.



          this.state.buttons.map(eachButtonNumber => 
          <button
          className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
          key=eachButtonNumber.id
          data-id=eachButtonNumber.id
          onClick=this.handleChange
          >
          &nbsp;
          </button>
          )





          share|improve this answer

























          • Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

            – Ankur Sehdev
            Mar 27 at 14:09












          • It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

            – BugsArePeopleToo
            Mar 27 at 14:13











          • I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

            – Ankur Sehdev
            Mar 27 at 14:21














          1












          1








          1







          If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6] for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:



          [
          id: 1, buttonName: 'purple',
          id: 2, buttonName: 'cyan',
          id: 3, buttonName: 'ballons',
          id: 4, buttonName: 'clouds',
          id: 5, buttonName: 'thumbs',
          id: 6, buttonName: 'smile',
          ]


          Then in your map loop, each button is checking if the selectedClass state matches its buttonName property and adding an active class in that case. Note you will need to point to id property now for the numeric id of each button.



          this.state.buttons.map(eachButtonNumber => 
          <button
          className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
          key=eachButtonNumber.id
          data-id=eachButtonNumber.id
          onClick=this.handleChange
          >
          &nbsp;
          </button>
          )





          share|improve this answer













          If your buttons have a little more detail to them this is pretty easy. If instead of [1,2,3,4,5,6] for your buttons, if the array had an object for each button, each button could have properties that can be matched to the state. So maybe like this:



          [
          id: 1, buttonName: 'purple',
          id: 2, buttonName: 'cyan',
          id: 3, buttonName: 'ballons',
          id: 4, buttonName: 'clouds',
          id: 5, buttonName: 'thumbs',
          id: 6, buttonName: 'smile',
          ]


          Then in your map loop, each button is checking if the selectedClass state matches its buttonName property and adding an active class in that case. Note you will need to point to id property now for the numeric id of each button.



          this.state.buttons.map(eachButtonNumber => 
          <button
          className=this.state.selectedClass === eachButtonNumber.buttonName && 'active'
          key=eachButtonNumber.id
          data-id=eachButtonNumber.id
          onClick=this.handleChange
          >
          &nbsp;
          </button>
          )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 4:48









          BugsArePeopleTooBugsArePeopleToo

          2,0021 gold badge9 silver badges12 bronze badges




          2,0021 gold badge9 silver badges12 bronze badges















          • Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

            – Ankur Sehdev
            Mar 27 at 14:09












          • It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

            – BugsArePeopleToo
            Mar 27 at 14:13











          • I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

            – Ankur Sehdev
            Mar 27 at 14:21


















          • Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

            – Ankur Sehdev
            Mar 27 at 14:09












          • It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

            – BugsArePeopleToo
            Mar 27 at 14:13











          • I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

            – Ankur Sehdev
            Mar 27 at 14:21

















          Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

          – Ankur Sehdev
          Mar 27 at 14:09






          Yes, I have full control over that array. And do you think this is the right approach to write React app in general? I mean the full application. and Thanks

          – Ankur Sehdev
          Mar 27 at 14:09














          It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

          – BugsArePeopleToo
          Mar 27 at 14:13





          It's the most React-y way to manage conditional classes in a loop that I know of. Happy to help!

          – BugsArePeopleToo
          Mar 27 at 14:13













          I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

          – Ankur Sehdev
          Mar 27 at 14:21






          I am getting a warning after using this: So I am using the ternary operator. Following is the warning. > Warning: Received false for a non-boolean attribute className. If you want to write it to the DOM, pass a string instead: className="false" or className=value.toString(). If you used to conditionally omit it with className=condition && value, pass className=condition ? value : undefined instead.

          – Ankur Sehdev
          Mar 27 at 14:21














          0














          Simplest way to do this is to get the className to be applied inside the render before returning the jsx.



          Like in render you can write the condition as below



          let classNameStyle = "" 
          if(this.state.displayBtns)
          classNameStyle = 'closeBtn '
          else
          classNameStyle = 'openBtn '

          if(this.state.active)
          classNameStyle = classNameStyle + 'active'


          Now apply this classNameStyle value to className property of button.
          (Note ) See the space in assignment of first className






          share|improve this answer

























          • Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

            – Ankur Sehdev
            Mar 27 at 4:26
















          0














          Simplest way to do this is to get the className to be applied inside the render before returning the jsx.



          Like in render you can write the condition as below



          let classNameStyle = "" 
          if(this.state.displayBtns)
          classNameStyle = 'closeBtn '
          else
          classNameStyle = 'openBtn '

          if(this.state.active)
          classNameStyle = classNameStyle + 'active'


          Now apply this classNameStyle value to className property of button.
          (Note ) See the space in assignment of first className






          share|improve this answer

























          • Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

            – Ankur Sehdev
            Mar 27 at 4:26














          0












          0








          0







          Simplest way to do this is to get the className to be applied inside the render before returning the jsx.



          Like in render you can write the condition as below



          let classNameStyle = "" 
          if(this.state.displayBtns)
          classNameStyle = 'closeBtn '
          else
          classNameStyle = 'openBtn '

          if(this.state.active)
          classNameStyle = classNameStyle + 'active'


          Now apply this classNameStyle value to className property of button.
          (Note ) See the space in assignment of first className






          share|improve this answer













          Simplest way to do this is to get the className to be applied inside the render before returning the jsx.



          Like in render you can write the condition as below



          let classNameStyle = "" 
          if(this.state.displayBtns)
          classNameStyle = 'closeBtn '
          else
          classNameStyle = 'openBtn '

          if(this.state.active)
          classNameStyle = classNameStyle + 'active'


          Now apply this classNameStyle value to className property of button.
          (Note ) See the space in assignment of first className







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 4:18









          Raj SaraogiRaj Saraogi

          2672 silver badges11 bronze badges




          2672 silver badges11 bronze badges















          • Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

            – Ankur Sehdev
            Mar 27 at 4:26


















          • Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

            – Ankur Sehdev
            Mar 27 at 4:26

















          Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

          – Ankur Sehdev
          Mar 27 at 4:26






          Hello Raj, I am trying to apply active class to ``` <button key=eachButtonNumber data-id=eachButtonNumber onClick=this.handleChange >&nbsp;</button> not the openBtn and CloseBtn. There is a list of buttons from the buttons.map.

          – Ankur Sehdev
          Mar 27 at 4:26


















          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%2f55369669%2fhow-to-add-active-class-to-a-list-of-buttons-onclick-they-are-not-controlled-by%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현