Skip to content

Commit 47ca335

Browse files
authored
added example for FC with default props (#222)
1 parent 2928b49 commit 47ca335

11 files changed

+145
-9
lines changed

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
9090
- [React](#react)
9191
- [Function Components - FC](#function-components---fc)
9292
- [- Counter Component](#--counter-component)
93+
- [- Counter Component with default props](#--counter-component-with-default-props)
9394
- [- Spreading attributes in Component](#--spreading-attributes-in-component)
9495
- [Class Components](#class-components)
9596
- [- Class Counter Component](#--class-counter-component)
@@ -325,6 +326,46 @@ export const FCCounter: React.FC<Props> = props => {
325326
326327
[⇧ back to top](#table-of-contents)
327328
329+
### - Counter Component with default props
330+
331+
```tsx
332+
import * as React from 'react';
333+
334+
type Props = {
335+
label: string;
336+
count: number;
337+
onIncrement: () => void;
338+
};
339+
340+
// React.FC is unaplicable here due not working properly with default props
341+
// https://github.com/facebook/create-react-app/pull/8177
342+
export const FCCounterWithDefaultProps = (props: Props): JSX.Element => {
343+
const { label, count, onIncrement } = props;
344+
345+
const handleIncrement = () => {
346+
onIncrement();
347+
};
348+
349+
return (
350+
<div>
351+
<span>
352+
{label}: {count}
353+
</span>
354+
<button type="button" onClick={handleIncrement}>
355+
{`Increment`}
356+
</button>
357+
</div>
358+
);
359+
};
360+
361+
FCCounterWithDefaultProps.defaultProps = { count: 5 };
362+
363+
```
364+
365+
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#fccounterwithdefaultprops)
366+
367+
[⇧ back to top](#table-of-contents)
368+
328369
### - [Spreading attributes](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes) in Component
329370
330371
```tsx

README_SOURCE.md

+9
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ I highly recommend to add a bounty to the issue that you're waiting for to incre
9090
- [React](#react)
9191
- [Function Components - FC](#function-components---fc)
9292
- [- Counter Component](#--counter-component)
93+
- [- Counter Component with default props](#--counter-component-with-default-props)
9394
- [- Spreading attributes in Component](#--spreading-attributes-in-component)
9495
- [Class Components](#class-components)
9596
- [- Class Counter Component](#--class-counter-component)
@@ -297,6 +298,14 @@ In code above `React.MouseEvent<HTMLDivElement>` is type of mouse event, and thi
297298
298299
[⇧ back to top](#table-of-contents)
299300
301+
### - Counter Component with default props
302+
303+
::codeblock='playground/src/components/fc-counter-with-default-props.tsx'::
304+
305+
[⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#fccounterwithdefaultprops)
306+
307+
[⇧ back to top](#table-of-contents)
308+
300309
### - [Spreading attributes](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes) in Component
301310
302311
::codeblock='playground/src/components/fc-spread-attributes.tsx'::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Storyshots FCCounterWithDefaultProps default 1`] = `
4+
<div>
5+
<span>
6+
FCCounterWithDefaultProps
7+
:
8+
5
9+
</span>
10+
<button
11+
onClick={[Function]}
12+
type="button"
13+
>
14+
Increment
15+
</button>
16+
</div>
17+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Usage:
2+
```jsx { "filePath": "./fc-counter-with-default-props.usage.tsx" }
3+
```
4+
5+
Usage Demo:
6+
```jsx
7+
const Demo = require('./fc-counter-with-default-props.usage').default;
8+
<Demo />
9+
```
10+
11+
[⇦ back to guide](https://github.com/piotrwitek/react-redux-typescript-guide#--fc-counter-with-default-props)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import React from 'react';
3+
import { storiesOf } from '@storybook/react';
4+
import { action } from '@storybook/addon-actions';
5+
6+
import { FCCounterWithDefaultProps } from '.';
7+
8+
storiesOf('FCCounterWithDefaultProps', module).add('default', () => (
9+
<FCCounterWithDefaultProps
10+
label={'FCCounterWithDefaultProps'}
11+
onIncrement={action('onIncrement')}
12+
/>
13+
));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as React from 'react';
2+
3+
type Props = {
4+
label: string;
5+
count: number;
6+
onIncrement: () => void;
7+
};
8+
9+
// React.FC is unaplicable here due not working properly with default props
10+
// https://github.com/facebook/create-react-app/pull/8177
11+
export const FCCounterWithDefaultProps = (props: Props): JSX.Element => {
12+
const { label, count, onIncrement } = props;
13+
14+
const handleIncrement = () => {
15+
onIncrement();
16+
};
17+
18+
return (
19+
<div>
20+
<span>
21+
{label}: {count}
22+
</span>
23+
<button type="button" onClick={handleIncrement}>
24+
{`Increment`}
25+
</button>
26+
</div>
27+
);
28+
};
29+
30+
FCCounterWithDefaultProps.defaultProps = { count: 5 };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { action } from '@storybook/addon-actions';
2+
import * as React from 'react';
3+
4+
import { FCCounterWithDefaultProps } from '.';
5+
6+
export default () => (
7+
<FCCounterWithDefaultProps
8+
label={'FCCounterWithDefaultProps'}
9+
onIncrement={action('onIncrement')}
10+
/>
11+
);

playground/src/components/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from './error-message';
22
export * from './generic-list';
33
export * from './fc-counter';
4-
export * from './fc-forward-ref';
4+
export * from './fc-counter-with-default-props';
55
export * from './fc-spread-attributes';
66
export * from './class-counter';
77
export * from './class-counter-with-default-props';

playground/src/routes/home.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import FCCounterUsage from '../components/fc-counter.usage';
3+
import FCCounterWithDefaultPropsUsage from '../components/fc-counter-with-default-props.usage';
34
import FCSpreadAttributesUsage from '../components/fc-spread-attributes.usage';
45
import ClassCounterUsage from '../components/class-counter.usage';
56
import ClassCounterWithDefaultPropsUsage from '../components/class-counter-with-default-props.usage';
@@ -12,6 +13,7 @@ export function Home() {
1213
return (
1314
<section>
1415
<FCCounterUsage />
16+
<FCCounterWithDefaultPropsUsage />
1517
<FCSpreadAttributesUsage />
1618
<ClassCounterUsage />
1719
<ClassCounterWithDefaultPropsUsage />
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// import initStoryshots, {
2+
// multiSnapshotWithOptions,
3+
// } from '@storybook/addon-storyshots';
4+
5+
// initStoryshots({
6+
// integrityOptions: { cwd: __dirname },
7+
// test: multiSnapshotWithOptions({}),
8+
// });
9+
10+
export {}

playground/src/storyshots.test.ts

-8
This file was deleted.

0 commit comments

Comments
 (0)