@@ -30,6 +30,7 @@ You should check Playground Project located in the `/playground` folder. It is a
30
30
- [ Stateless Components - SFC] ( #stateless-components---sfc )
31
31
- [ Stateful Components - Class] ( #stateful-components---class ) 📝 __ UPDATED__
32
32
- [ Generic Components] ( #generic-components )
33
+ - [ Render Props] ( #render-props ) 🌟 __ NEW__
33
34
- [ Higher-Order Components] ( #higher-order-components ) 📝 __ UPDATED__
34
35
- [ Redux Connected Components] ( #redux-connected-components )
35
36
- [ Redux] ( #redux )
@@ -176,7 +177,7 @@ export const SFCCounter: React.SFC<SFCCounterProps> = (props) => {
176
177
177
178
[⇧ back to top](#table-of-contents)
178
179
179
- #### - spreading attributes [link](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes)
180
+ #### - spread attributes [link](https://facebook.github.io/react/docs/jsx-in-depth.html#spread-attributes)
180
181
181
182
```tsx
182
183
import * as React from ' react' ;
@@ -348,10 +349,88 @@ export class GenericList<T> extends React.Component<GenericListProps<T>, {}> {
348
349
349
350
---
350
351
352
+ ## Render Props
353
+ > https://reactjs.org/docs/render-props.html
354
+
355
+ #### - name provider
356
+ > simple component using children as a render prop
357
+
358
+ ```tsx
359
+ import * as React from 'react';
360
+
361
+ interface NameProviderProps {
362
+ children : (state : NameProviderState ) => React .ReactNode ;
363
+ }
364
+
365
+ interface NameProviderState {
366
+ name : string ;
367
+ }
368
+
369
+ export class NameProvider extends React.Component<NameProviderProps, NameProviderState> {
370
+ state = {
371
+ name: ' Piotr' ,
372
+ };
373
+
374
+ render () {
375
+ return this.props.children(this.state);
376
+ }
377
+ }
378
+
379
+ ```
380
+
381
+ [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#nameprovider)
382
+
383
+ [⇧ back to top](#table-of-contents)
384
+
385
+ #### - mouse provider
386
+ > `Mouse` component found in [Render Props React Docs](https://reactjs.org/docs/render-props.html#use-render-props-for-cross-cutting-concerns)
387
+
388
+ ```tsx
389
+ import * as React from 'react';
390
+
391
+ export interface MouseProviderProps {
392
+ render : (state : MouseProviderState ) => React .ReactNode ;
393
+ }
394
+
395
+ interface MouseProviderState {
396
+ x : number ;
397
+ y : number ;
398
+ }
399
+
400
+ export class MouseProvider extends React.Component<MouseProviderProps, MouseProviderState> {
401
+ state = { x: 0 , y: 0 };
402
+
403
+ handleMouseMove = (event : React .MouseEvent <any >) => {
404
+ this .setState ({
405
+ x: event .clientX ,
406
+ y: event .clientY ,
407
+ });
408
+ }
409
+
410
+ render () {
411
+ return (
412
+ <div style = {{ height : ' 100%' }} onMouseMove = {this.handleMouseMove } >
413
+
414
+ {/*
415
+ Instead of providing a static representation of what <Mouse> renders,
416
+ use the `render` prop to dynamically determine what to render.
417
+ */ }
418
+ {this .props .render (this .state )}
419
+ </div >
420
+ );
421
+ }
422
+ }
423
+
424
+ ```
425
+
426
+ [⟩⟩⟩ demo](https://piotrwitek.github.io/react-redux-typescript-guide/#mouseprovider)
427
+
428
+ [⇧ back to top](#table-of-contents)
429
+
430
+ ---
431
+
351
432
## Higher-Order Components
352
- - function that takes a component and returns a new component
353
- - a new component will infer Props interface from wrapped Component extended with Props of HOC
354
- - will filter out props specific to HOC, and the rest will be passed through to wrapped component
433
+ > https://reactjs.org/docs/higher-order-components.html
355
434
356
435
#### - withState
357
436
Adds state to a stateless counter
0 commit comments