问题出在在React中切换图片和文本之间。

huangapple go评论70阅读模式
英文:

problem with switching between img and text in react

问题

It seems like you want to toggle between displaying an image and text when clicking on buttons, but you're encountering an issue where clicking on the same button a second time returns to displaying the image. To achieve the desired behavior, you can modify your code as follows:

First, update your SectionFour component to handle the toggle logic more effectively:

import React, { useState } from 'react';
import './home-css/sectionthree.css';
import secThee from './Home-assets/sec3.jpg';

const SectionFour = () => {
  const [activeButton, setActiveButton] = useState(0);

  const handleButtonClick = (buttonNumber) => {
    setActiveButton((prevActiveButton) => (prevActiveButton === buttonNumber ? 0 : buttonNumber));
  };

  return (
    <div className='section-three-container'>
      <div className='section-three grid-system'>
        <div className='benefits-text'>
          <p className='ben-title'>buttons</p>
          <div className='sub-benefits'>
            {[1, 2, 3, 4, 5, 6].map((buttonNumber) => (
              <div key={buttonNumber} className={`sub-ben text-${buttonNumber}`}>
                <button onClick={() => handleButtonClick(buttonNumber)} className='sub-ben-text'>
                  <span></span>
                  <span></span>
                  <span></span>
                  <span></span>
                  {buttonNumber}
                </button>
              </div>
            ))}
          </div>
        </div>

        {activeButton === 0 ? (
          <img src={secThee} draggable="false" />
        ) : (
          <div className={`hidden-content-${activeButton} hidden-content`}>
            <p className='hidden-title'>Text{activeButton}</p>
            <p className='hidden-para'>Text{activeButton}</p>
          </div>
        )}
      </div>
    </div>
  );
};

export default SectionFour;

In this modified code, we maintain the active button's state (activeButton) and use a function to toggle it. When a button is clicked, it checks if the clicked button is the same as the currently active button; if it is, it toggles back to 0 (displaying the image). Otherwise, it updates to the clicked button number, showing the corresponding text. This should prevent the issue you were facing with clicking the same button twice.

英文:
import React, {useState} from &#39;react&#39;
import &#39;./home-css/sectionthree.css&#39;
import secThee from &#39;./Home-assets/sec3.jpg&#39;
const img = ()=&gt;{
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;;
}
const secTwo = (value,active) =&gt; {
if(active){
switch (value) {
case value 0:
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;
break;
case value 1:
return(&lt;div className=&#39;hidden-content-one hidden-content&#39;&gt;
&lt;p className=&#39;hidden-title&#39;&gt;Text1&lt;/p&gt;
&lt;p className=&#39;hidden-para&#39;&gt;Text1&lt;/p&gt;
&lt;/div&gt;)
break;
case value 2:
return(
&lt;div className=&#39;hidden-content-two hidden-content&#39;&gt;
&lt;p className=&#39;hidden-title&#39;&gt;Text2&lt;/p&gt;
&lt;p className=&#39;hidden-para&#39;&gt;Text2&lt;/p&gt;
&lt;/div&gt;)
break;
case value 3:
return(&lt;div className=&#39;hidden-content-three hidden-content&#39;&gt;
&lt;p className=&#39;hidden-title&#39;&gt;Text3&lt;/p&gt;
&lt;p className=&#39;hidden-para&#39;&gt;Text3&lt;/p&gt;
&lt;/div&gt;)
break;
case value 4:
return(&lt;div className=&#39;hidden-content-four hidden-content&#39;&gt;
&lt;p className=&#39;hidden-title&#39;&gt;Text4&lt;/p&gt;
&lt;p className=&#39;hidden-para&#39;&gt;Text4&lt;/p&gt;
&lt;/div&gt;)
break;
case value 5:
return(&lt;div className=&#39;hidden-content-five hidden-content&#39;&gt;
&lt;p className=&#39;hidden-title&#39;&gt;Text5&lt;/p&gt;
&lt;p className=&#39;hidden-para&#39;&gt;Text5&lt;/p&gt;
&lt;/div&gt;)
break;
case value 6:
return(&lt;div className=&#39;hidden-content-six hidden-content&#39;&gt;
&lt;p className=&#39;hidden-title&#39;&gt;Text6&lt;/p&gt;
&lt;p className=&#39;hidden-para&#39;&gt;Text6&lt;/p&gt;
&lt;/div&gt;)
break;
default:
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;
break;
}
}else{
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;
}
}
const hellobe = (buttonValue,buttonActive) =&gt;{
console.log(buttonValue,buttonActive);
}
const SectionFour = () =&gt; {
const [hello,setHello] =useState({value:0,active:false});
const buttonOne = () =&gt;{
setHello({...hello,value:1,active:!hello.active});
return hello.value,hello.active;
}
const buttonTwo = () =&gt;{
setHello({...hello,value:2,active:!hello.active});
return hello.value,hello.active;
}
const buttonThree = () =&gt;{
setHello({...hello,value:3,active:!hello.active});
return hello.value,hello.active;
}
const buttonFour = () =&gt;{
setHello({...hello,value:4,active:!hello.active});
return hello.value,hello.active;
}
const buttonFive = () =&gt;{
setHello({...hello,value:5,active:!hello.active});
return hello.value,hello.active;
}
const buttonSix = () =&gt;{
setHello({...hello,value:6,active:!hello.active});
return hello.value,hello.active;
}
return (
&lt;div className=&#39;section-three-container&#39;&gt;
&lt;div className=&#39;section-three grid-system&#39;&gt;
&lt;div className=&#39;benefits-text&#39;&gt;
&lt;p className=&#39;ben-title&#39;&gt;buttons&lt;/p&gt;
&lt;div className=&#39;sub-benefits&#39;&gt;
&lt;div className=&#39;sub-ben text-one&#39;&gt;
&lt;button onClick={buttonOne} className=&#39;sub-ben-text&#39;&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
1
&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&#39;sub-ben text-two&#39;&gt;
&lt;button onClick={buttonTwo} className=&#39;sub-ben-text&#39;&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
2
&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&#39;sub-ben text-three&#39;&gt;
&lt;button onClick={buttonThree} className=&#39;sub-ben-text&#39;&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
3
&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&#39;sub-ben text-four&#39;&gt;
&lt;button onClick={buttonFour} className=&#39;sub-ben-text&#39;&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
4
&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&#39;sub-ben text-five&#39;&gt;
&lt;button onClick={buttonFive} className=&#39;sub-ben-text&#39;&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
5
&lt;/button&gt;
&lt;/div&gt;
&lt;div className=&#39;sub-ben text-six&#39;&gt;
&lt;button onClick={buttonSix} className=&#39;sub-ben-text&#39;&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
&lt;span&gt;&lt;/span&gt;
6
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
{hellobe(hello.value,hello.active)}
{secTwo(hello.value,hello.active)}
&lt;/div&gt;
&lt;/div&gt;
)
}
export default SectionFour

I am trying to change from img to text by clicking on one of the buttons in the code below. But when I click on the same button a second time it returns to the img.

When I click on a button, it changes it to img as expected, but when I click on any other button a second time, even the same it returns to the img.

I tried to compare to the previous value of the state but I don't think I did it right

答案1

得分: 1

以下是翻译好的内容:

在代码中存在语法错误。在switch语句中,case不应在括号内包含赋值操作(=)。

const secTwo = (value, active) => {
  if (active) {
    switch (value) {
      case 0:
        return <img src={secThee} draggable="false" />;
      case 1:
        return (
          <div className="hidden-content-one hidden-content">
            <p className="hidden-title">title1</p>
            <p className="hidden-para">title 1 para</p>
          </div>
        );
      case 2:
        return (
          <div className="hidden-content-two hidden-content">
            <p className="hidden-title">title 2</p>
            <p className="hidden-para">title 2 para</p>
          </div>
        );
      case 3:
        return (
          <div className="hidden-content-three hidden-content">
            <p>title 3</p>
          </div>
        );
      case 4:
        return (
          <div className="hidden-content-four hidden-content">
            <p>title 4</p>
          </div>
        );
      case 5:
        return (
          <div className="hidden-content-five hidden-content">
            <p>title 5</p>
          </div>
        );
      case 6:
        return (
          <div className="hidden-content-six hidden-content">
            <p>title 6</p>
          </div>
        );
      default:
        return <img src={secThee} draggable="false" />;
    }
  } else {
    return <img src={secThee} draggable="false" />;
  }
};
英文:

There is a syntax error in the code. In the switch statement, the cases should not have an assignment operation (=) inside the parentheses.

const secTwo = (value, active) =&gt; {
if (active) {
switch (value) {
case 0:
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;;
case 1:
return (
&lt;div className=&quot;hidden-content-one hidden-content&quot;&gt;
&lt;p className=&quot;hidden-title&quot;&gt;title1&lt;/p&gt;
&lt;p className=&quot;hidden-para&quot;&gt;title 1 para&lt;/p&gt;
&lt;/div&gt;
);
case 2:
return (
&lt;div className=&quot;hidden-content-two hidden-content&quot;&gt;
&lt;p className=&quot;hidden-title&quot;&gt;title 2&lt;/p&gt;
&lt;p className=&quot;hidden-para&quot;&gt;title 2 para&lt;/p&gt;
&lt;/div&gt;
);
case 3:
return (
&lt;div className=&quot;hidden-content-three hidden-content&quot;&gt;
&lt;p&gt;title 3&lt;/p&gt;
&lt;/div&gt;
);
case 4:
return (
&lt;div className=&quot;hidden-content-four hidden-content&quot;&gt;
&lt;p&gt;title 4&lt;/p&gt;
&lt;/div&gt;
);
case 5:
return (
&lt;div className=&quot;hidden-content-five hidden-content&quot;&gt;
&lt;p&gt;title 5&lt;/p&gt;
&lt;/div&gt;
);
case 6:
return (
&lt;div className=&quot;hidden-content-six hidden-content&quot;&gt;
&lt;p&gt;title 6&lt;/p&gt;
&lt;/div&gt;
);
default:
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;;
}
} else {
return &lt;img src={secThee} draggable=&quot;false&quot; /&gt;;
}
};

huangapple
  • 本文由 发表于 2023年5月11日 00:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定