javascript - Changing text color on click with react hooks - Stack Overflow

I have a simple section where user can click a button, now I want on click to change (toggle) the color

I have a simple section where user can click a button, now I want on click to change (toggle) the color of the text using react hooks here is what I have so far.'

const [textColor, setTextColor] = useState('black');

 const handleChnageTextColor = (e) => {
    setTextColor('#CCCCCC');
}

return(
<>
<label onClick={handleChnageTextColor} className="switch">
        <input type="checkbox" />
        <span className="slider round" />
</label>

 <small style={{ color:textColor}} className="switch-container_text">advanced view</small>
</>
)

so the initial color is black on click I am changing color to #CCCCCC now when I click again the color is not changing.

What do I need to add to make this toggle between these two colors on click?

I have a simple section where user can click a button, now I want on click to change (toggle) the color of the text using react hooks here is what I have so far.'

const [textColor, setTextColor] = useState('black');

 const handleChnageTextColor = (e) => {
    setTextColor('#CCCCCC');
}

return(
<>
<label onClick={handleChnageTextColor} className="switch">
        <input type="checkbox" />
        <span className="slider round" />
</label>

 <small style={{ color:textColor}} className="switch-container_text">advanced view</small>
</>
)

so the initial color is black on click I am changing color to #CCCCCC now when I click again the color is not changing.

What do I need to add to make this toggle between these two colors on click?

Share Improve this question edited Jul 27, 2020 at 14:39 The Dead Man asked Jul 27, 2020 at 10:16 The Dead ManThe Dead Man 5,57633 gold badges125 silver badges226 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Change your handleChangeTextColor to following:

const handleChnageTextColor = (e) => {

 setTextColor(textColor === 'black' ? '#CCCCCC' : 'black');
}

You should assign the value of checkbox with some state variable and make the color of the text dependant on the checkbox value. Following will surely help you achieve the desired results.

const [textColor, setTextColor] = useState('black');
const [isBlack, setIsBlack] = useState(true);

const handleChnageTextColor = (e) => {
  setIsBlack(!isBlack);
  setTextColor(isBlack ? '#CCCCCC' : 'black ');
}

return(
  <>
   <label className="switch">
     <input type="checkbox" value={isBlack} onChange={handleChnageTextColor} />
     <span className="slider round" />
   </label>
   <small style={{ color:textColor}} className="switch-container_text">advanced view</small>
  </>
 )
}

You should check the element style value to decide the color to apply:

if(e.style.color === "black") '#CCCCCC' : 'black'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744216067a4563559.html

相关推荐

  • javascript - Changing text color on click with react hooks - Stack Overflow

    I have a simple section where user can click a button, now I want on click to change (toggle) the color

    8天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信