|
| 1 | +// https://github.com/47ng/next-usequerystate/issues/359 |
| 2 | + |
| 3 | +'use client' |
| 4 | + |
| 5 | +import { |
| 6 | + parseAsString, |
| 7 | + parseAsStringEnum, |
| 8 | + useQueryState, |
| 9 | + useQueryStates |
| 10 | +} from '../../../../dist' |
| 11 | + |
| 12 | +const Component1 = () => { |
| 13 | + const [param] = useQueryState('param', parseAsString) |
| 14 | + console.dir({ _: 'Component1.render', param }) |
| 15 | + return param ? param : 'null' |
| 16 | +} |
| 17 | + |
| 18 | +const Component2 = () => { |
| 19 | + const [param] = useQueryState('param', parseAsString) |
| 20 | + console.dir({ _: 'Component2.render', param }) |
| 21 | + return param ? param : 'null' |
| 22 | +} |
| 23 | + |
| 24 | +enum TargetComponent { |
| 25 | + Comp1 = 'comp1', |
| 26 | + Comp2 = 'comp2' |
| 27 | +} |
| 28 | + |
| 29 | +export default function Home() { |
| 30 | + const [_param, setParam] = useQueryState('param', parseAsString) |
| 31 | + const [component, seComponent] = useQueryState( |
| 32 | + 'component', |
| 33 | + parseAsStringEnum(Object.values(TargetComponent)) |
| 34 | + ) |
| 35 | + const [multiple, setMultiple] = useQueryStates({ |
| 36 | + param: parseAsString, |
| 37 | + component: parseAsStringEnum(Object.values(TargetComponent)) |
| 38 | + }) |
| 39 | + console.dir({ _: 'Home.render', _param, component, multiple }) |
| 40 | + return ( |
| 41 | + <> |
| 42 | + <h1> |
| 43 | + Repro for issue{' '} |
| 44 | + <a href="https://github.com/47ng/next-usequerystate/issues/359">#359</a> |
| 45 | + </h1> |
| 46 | + <div className="p-5 border"> |
| 47 | + {component === TargetComponent.Comp1 ? <Component1 /> : null} |
| 48 | + {component === TargetComponent.Comp2 ? <Component2 /> : null} |
| 49 | + </div> |
| 50 | + <div className="flex gap-2"> |
| 51 | + <button |
| 52 | + onClick={() => { |
| 53 | + setParam('Component1') |
| 54 | + seComponent(TargetComponent.Comp1) |
| 55 | + }} |
| 56 | + className="border p-2" |
| 57 | + > |
| 58 | + Component 1 (nuqs) |
| 59 | + </button> |
| 60 | + <button |
| 61 | + onClick={() => { |
| 62 | + console.log('aaa') |
| 63 | + setParam('Component2') |
| 64 | + seComponent(TargetComponent.Comp2) |
| 65 | + }} |
| 66 | + className="border p-2" |
| 67 | + > |
| 68 | + Component 2 (nuqs) |
| 69 | + </button> |
| 70 | + <br /> |
| 71 | + <button |
| 72 | + onClick={() => { |
| 73 | + setMultiple({ |
| 74 | + param: 'Component1', |
| 75 | + component: TargetComponent.Comp1 |
| 76 | + }) |
| 77 | + }} |
| 78 | + className="border p-2" |
| 79 | + > |
| 80 | + Component 1 (nuq+) |
| 81 | + </button> |
| 82 | + <button |
| 83 | + onClick={() => { |
| 84 | + setMultiple({ |
| 85 | + param: 'Component2', |
| 86 | + component: TargetComponent.Comp2 |
| 87 | + }) |
| 88 | + }} |
| 89 | + className="border p-2" |
| 90 | + > |
| 91 | + Component 2 (nuq+) |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + <p> |
| 95 | + <a href="https://github.com/47ng/next-usequerystate/blob/next/src/app/demos/repro-359/page.tsx"> |
| 96 | + Source on GitHub |
| 97 | + </a> |
| 98 | + </p> |
| 99 | + </> |
| 100 | + ) |
| 101 | +} |
0 commit comments