React Js Load More onclick button
React Js Load More onclick button:In React.js, implementing a "Load More" functionality with an onClick button involves creating a state variable to track the number of items displayed. Initially, a certain number of items are rendered, and when the button is clicked, the state is updated to render more items, typically by incrementing the count. This triggers a re-render, displaying additional content.
This approach enhances user experience by loading content incrementally, reducing initial page load times, and allowing users to control when more data is loaded. It's a common pattern for managing large data sets in web applications, offering a responsive and efficient way to fetch and display additional information as needed.




Thanks for your feedback!
Your contributions will help us to improve service.
How do I create a "Load More" button in Reactjs to fetch and display more items on click?
This React.js code snippet creates a dynamic item list with a "Load More" button. Initially, it displays 2 items from an array of demo text items. When the button is clicked, it triggers the loadMoreItems
function, which simulates a loading state for 1 second and then shows 2 more items from the array. A loading message is displayed while loading. The useState
hook is used to manage item count and loading state. This interactive component allows users to progressively load more items from the list on-demand, improving user experience by reducing initial content overload.
React Js Load More Item onclick button Example
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
function App() {
const [itemsToShow, setItemsToShow] = useState(2); // Initially, display 5 items
const [loading, setLoading] = useState(false); // Loading state
// Demo text items
const items = [
'Argentina, Population: 45 million',
'Chile, Population: 19 million',
'Colombia, Population: 51 million',
'Peru, Population: 33 million',
];
// Function to load more items
const loadMoreItems = () => {
setLoading(true); // Set loading to true when loading starts
setTimeout(() => {
setItemsToShow(itemsToShow + 2); // Load 2 more items at a time
setLoading(false); // Set loading to false when loading is complete
}, 1000); // Simulate loading delay (you can adjust this)
};
return (
<div className='container'>
<h3 className='heading'>React Js Load More onclick Button</h3>
<ul className='item-list'>
{items.slice(0, itemsToShow).map((item, index) => (
<li key={index} className='list-item'>{item}</li>
))}
</ul>
{loading ? (
<p className='loading-text'>Loading...</p> // Display a loading message or spinner while loading
) : (
itemsToShow < items.length && (
<button onClick={loadMoreItems} className='load-more-button'>Load More</button>
)
)}
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>