UNPKG

1.66 kBMarkdownView Raw
1# storybook-addon-queryparams
2
3This storybook addon can be helpful if your components need special query parameters to work the way you want them to. It allows you to mock query params per story so that you can easily reproduce different states of your component.
4
5## Getting started
6
7First, install the addon.
8
9```sh
10$ yarn add @storybook/addon-queryparams --dev
11```
12
13Register it by adding it in the addons attribute in your `main.js` file (create this file inside your storybook config directory if needed).
14
15```js
16module.exports = {
17 addons: ['@storybook/addon-queryparams'],
18};
19```
20
21In your story, add the `withQuery` decorator and define the query parameters you want to mock:
22
23```js
24import React from 'react';
25import { Button } from '@storybook/react/demo';
26import { withQuery } from '@storybook/addon-queryparams';
27
28export default {
29 title: 'Button',
30 component: Button,
31 decorators: [withQuery],
32 parameters: {
33 query: {
34 mock: 'Hello world!',
35 },
36 },
37};
38
39export const WithMockedSearch = () => {
40 const urlParams = new URLSearchParams(document.location.search);
41 const mockedParam = urlParams.get('mock');
42 return <div>Mocked value: {mockedParam}</div>;
43};
44```
45
46<details>
47 <summary>Example with storiesOf API</summary>
48
49```js
50import React from 'react';
51import { storiesOf } from '@storybook/react';
52
53storiesOf('button', module)
54 .addParameters({
55 query: {
56 mock: 'Hello World!',
57 },
58 })
59 .add('Prints the mocked parameter', () => {
60 const urlParams = new URLSearchParams(document.location.search);
61 const mockedParam = urlParams.get('mock');
62 return <div>Mocked value: {mockedParam}</div>;
63 });
64```
65
66</details>