I’m trying to use useState react hook as follows,
const [showMore, setShowMore] = useState(false);
function handleClick(){
setShowMore(true);
}
I pass my wishlistItemCount value to WishlistItems component
const wishListItemCount = showMore ? itemsCount : 3;
const contentMessageElement =
<WishlistItems
itemCount={wishListItemCount}
wishlistId={data.id}
items={items}/>
I use following button to change the state
<Button
onClick={handleClick}
className={classes.btnshowmore}>Show More
</Button>
But I get the error when I load the page
Storybook preview hooks can only be called inside decorators and story functions.
Please help
My Full component
import React,{ useCallback } from 'react';
import { useMutation } from '@apollo/client';
import {FormattedMessage, useIntl} from 'react-intl';
import {ChevronDown, ChevronUp, MoreHorizontal} from 'react-feather';
import {useWishlist} from '@magento/peregrine/lib/talons/WishlistPage/useWishlist';
import {mergeClasses} from '@magento/venia-ui/lib/classify';
import Icon from '../Icon';
import WishlistItems from './wishlistItems';
import defaultClasses from './wishlist.css';
import {Button} from "react-bootstrap";
import {useWishlistAllItems} from "./useWishlistAllItems";
import wishlistItemOperations from "./wishlistItem.gql";
import {useCartContext} from "@magento/peregrine/lib/context/cart";
import {Link} from "../../drivers";
import {useState} from "@storybook/addons";
const Wishlist = props => {
const {data} = props;
const {formatMessage} = useIntl();
const {
items_count: itemsCount,
items: items,
name,
sharing_code: sharingCode
} = data;
const talProps = useWishlistAllItems({
items,
...wishlistItemOperations
});
const {
handleAddAllItemsToCart,
saveWishList,
hasError,
isLoading
} = talProps;
const talonProps = useWishlist();
const {isOpen} = talonProps;
const [showMore, setShowMore] = useState(false);
function handleClick(){
setShowMore(true);
}
const wishListItemCount = showMore ? itemsCount : 3;
const classes = mergeClasses(defaultClasses, props.classes);
const contentClass = isOpen ? classes.content : classes.content_hidden;
const visibilityLabel = sharingCode
? formatMessage({id: 'wishlist.publicText', defaultMessage: 'Public'})
: formatMessage({
id: 'wishlist.privateText',
defaultMessage: 'Private'
});
const contentMessageElement = itemsCount ? (
<WishlistItems
itemCount={wishListItemCount}
wishlistId={data.id}
items={items}/>
) : (
<p>
<FormattedMessage
id={'wishlist.emptyListText'}
defaultMessage={'There are currently no items in this list'}
/>
</p>
);
return (
<div className={classes.root}>
<div className={classes.header}>
<div className={classes.nameContainer}>
<h2 className={classes.name}>{name}</h2>
</div>
<div className={classes.subname}>
<span className={classes.visibility}>
{visibilityLabel}
</span>
<a className={classes.seeall}>See all</a>
</div>
</div>
<div className={classes.tableheadroot}>
<p className={classes.th1}>Product Information</p>
<p className={classes.th2}>Put in the Basket</p>
</div>
<div className={contentClass}>{contentMessageElement}</div>
<div className={classes.showmore}>
<Button
onClick={handleClick}
className={classes.btnshowmore}>Show More</Button>
</div>
<div className={classes.btnsetbottom}>
<Link to={'/share-wishlist'}>
<Button className={classes.btnsharewishlist}>
Share your wish list
</Button>
</Link>
<Button
className={classes.btnsharewishlist}
onClick={handleAddAllItemsToCart}>
Put all the goods in basket
</Button>
<Button
onclick={saveWishList}
className={classes.btnsharewishlist}>
Save your wish list
</Button>
</div>
</div>
);
};
export default Wishlist;
3
Answers
there is different in html events and react.
document description
You have 2 issues here, button on click invoke on render and you need to use storybook state to pass state changes
try changing
to