'How can i hide long description and making it toggle in react js?
I face a problem. my reactjs application has some card items and has some descriptions here. Here I want to make it a short description and a button, and when I click here then I see the full details, and when I click a second time it will be also hidden again, just like a toggle button for making short and long description
Solution 1:[1]
// Get a hook function
const {useState} = React;
const Example = ({title}) => {
const [showMore, setShowMore] = useState(false);
const text = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for lorein their infancy. Various versions have evolved over the years, sometimes by";
return (
<div className="App">
<p>
{showMore ? text : `${text.substring(0, 50)}`}
</p>
<button className="btn" onClick={() => setShowMore(!showMore)}>Show more</button>
</div>
);
};
// Render it
ReactDOM.render(
<Example title="Example using Hooks:" />,
document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gzen |