10

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;
15
  • 5
    <Button onClick={handleClick} ... Commented Nov 24, 2020 at 13:16
  • @Roy.B does'nt make any difference Commented Nov 24, 2020 at 13:20
  • Are you using a story in your storybook to show your new component? The problem looks like a storybook config problem, also Roy.B has a point in the onClick event from the button you should pass the reference or a function onClick={handleClick} or onClick={()=>handleClick()} Commented Nov 24, 2020 at 13:21
  • button click is not the issue.I have no idea about the storybooks Commented Nov 24, 2020 at 13:22
  • 1
    @Roy.B solved that issue too thank you Commented Nov 24, 2020 at 14:58

3 Answers 3

34

try changing

import {useState} from "@storybook/addons";

to

import {useState} from "react";
Sign up to request clarification or add additional context in comments.

3 Comments

I had this problem, is there any reason to import it from there?
it's VS code IntelliSense, thanks for the help
I need to do the opposite to make this work :shrug:
0
           <Button
                onClick={handleClick}
                className={classes.btnshowmore}>Show More
             </Button>

there is different in html events and react.

document description

1 Comment

does'nt make any difference I have updated my question
0

You have 2 issues here, button on click invoke on render and you need to use storybook state to pass state changes

const [showMoreWishList, setShowMoreWishList] = useAddonState(data.id, false); 
  ...

function handleClick(){ 
  setShowMore(true); 
  setShowMoreWishList(true); 
}
 ...

  <Button 
    onClick={handleClick}
 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.