score:2
Accepted answer
const Second = ({container}) => {
const [showMore, setShowMore] = useState(false);
return(
<div>
{container?.slice(0, 5).map((container) => (
<h3>{container}</h3>
))}
{showMore && container?.slice(5).map((container) => (
<h3>{container}</h3>
))} //this would show the remaining values
<button type="button" class="btn btn-primary" onClick={() => setShowMore(true)}>Primary</button>
</div>
)
}
score:2
function Second({container = []}) {
const [showAll, setShowAll] = useState(false);
function handleClick() {
setShowAll(prevShowAll => !prevShowAll);
}
const items = showAll ? container : container.slice(0, 5);
return(
<div>
{items.map(item => <h3>{item}</h3>)}
<button type="button" class="btn btn-primary" onClick={handleClick}>
{showAll ? "Show first five items" : "Show all items"}
</button>
</div>
)
}
Source: stackoverflow.com
Related Query
- How to load more items from a map function on click of a button
- How to load more data on click from Firestore
- How to fire a setState function in react from child componenet to parent componenet with data from a button click
- How to add a load more button to load next 50 requests from axios call?
- how to hide load more button after all data fetch from API?
- how to load a function when I click on a button in reactjs
- How to load a component from a button click in react?
- How to write a hook to load more button from fetching a json?
- How to spy on function from specific button click using sinon?
- How to add items returned from map function
- how to pass just the function from one component to other component without passing the whole component or button click
- How to load data from api and load component asynchronously with map function
- How to access more than one button from 1 onClick function in React?
- React: Button onClick function is running on page load but not you click it
- React - Fetch from external API function on button click
- How to set a defaultChecked to only first input in radio button when using map function in reactjs
- How to load a new component on button click in ReactJs?
- How to fetch data on page load and then on button click with useEffect
- How to delete objects from react state hook array with a button click
- How do I call a function on button click to listen for another mouse click in React Hooks?
- how to load another local js from a button
- React: how to access component function from a click event on vis.js edge?
- How to Create a dropdown Button with see more option and on click toggle should not happen in react js?
- Need Reactjs help on how to properly pass this function from the parent to child button component
- React: how to get item width from map function
- Not able to call function from React Js Button Click
- How can I limit items in the cart with map function (ReactJS)
- How to call a function and pass parameter in from a map function
- Brand new to react and confused on how I omit an item from a function and also inject a button into my grid
- How to load new component on button click
More Query from same tag
- Mapping Array in a Props
- How to find out if popupState from Material UI is opened
- Tailwindcss manual dark mode doesn't work in NextJS project
- How to loop this array?
- Show only an attribute, but save entire object (React Final Form + Downshift)
- Render a table body in react using the map function
- Set correct height of React component after scaling with tailwindcss
- React-Bootstrap Popover with Form field Validation
- which React component should i use with parameters?
- "Expected 1 arguments, but got 2" when passing a useState function in TypeScript
- How to use React.lazy in Gatsby
- Auth0 Lock v11 in React app not catching ‘authenticated’ event
- TypeScript Theme Switch types erros
- Should I use dispatch on return type of action in reactjs?
- Can I call HOC directly from within JSX / TSX in React?
- Is there a simple idiomatic way of updating key-value states in Redux reducers
- Deploying reactjs based application to AWS
- Set array items in react js state
- React Plaid Component Refreshes the page
- How to get acess to the query argument followed after a questionmark?
- React/Redux - Pass variable to action
- React - How to filter a list without losing data?
- Dynamic Background w/ tailwind
- Covert S3 Buffer Data to PDF File
- Best way to filter a geojson in react?
- Vercel React.js Deployment: Cannot find module: 'firebase'. Make sure this package is installed
- Is it acceptable to use object properties that way?
- React - Uncaught TypeError: Cannot read property 'state' of > null
- Add all search results to state React
- Using Jest+Enzyme+React does not point the right line on error