Guest User

Untitled

a guest
Feb 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. Children has an implicit contract:
  2.  
  3. Anything you pass in will be rendered as written
  4.  
  5. obvs react doesn't give us such a guarantee, but I think it's a helpful convention
  6.  
  7. - argument naming clarity/predictability
  8. ideally we name our arguments so it's clear what's happening from the outside
  9. `adopt(parent, child)`
  10.  
  11.  
  12. children means "stuff I will render as my main content"
  13. (main content is super up for interpretation)
  14.  
  15. `ContentFrame({}, children)`
  16. children means "some stuff I will render as my main content, some stuff I will selectively extract and render elsewhere,
  17. also if you pass two of the same special node I will only render one of them"
  18.  
  19. - ease of implementation
  20. - interface is limited to the things you can expect
  21. square block square hole
  22. you don't need type-checking and warnings because you can't even provide the wrong thing!
  23.  
  24. ```jsx
  25. const ContentFrame = ({title, children}) => {
  26. const childrenArray = React.Children.toArray(children);
  27. const possibleHeaderChildren = childrenArray.find(child => child.type === ContentFrameHeader);
  28. const defaultHeader = (
  29. <ContentFrameHeader>
  30. <Title>{title}</Title>
  31. </ContentFrameHeader>
  32. )
  33.  
  34. const header = possibleHeaderChildren || defaultHeader;
  35.  
  36. const actualChildren = childrenArray.filter(child => child.type !== ContentFrameHeader);
  37.  
  38. return (
  39. <div className="content-frame">
  40. { header }
  41. <ContentFrameBody>
  42. { actualChildren }
  43. </ContentFrameBody>
  44. </div>
  45. );
  46. }
  47. ```
  48.  
  49. consider usages:
  50.  
  51. ```jsx
  52. const justTitle = (
  53. <ContentFrame title="Hello">
  54. I am body content
  55. </ContentFrame>
  56. );
  57.  
  58. const customHeader = (
  59. <ContentFrame>
  60. <ContentFrameHeader>
  61. <Title>Hello</Title>
  62. </ContentFrameHeader>
  63. I am body content
  64. </ContentFrame>
  65. );
  66.  
  67. const trollFace = (
  68. <ContentFrame title="Hello">
  69. I am body content
  70. <ContentFrameHeader>
  71. <Title>I am your friend</Title>
  72. </ContentFrameHeader>
  73. <ContentFrameHeader>
  74. You will never see me again
  75. </ContentFrameHeader>
  76. </ContentFrame>
  77. );
  78. ```
  79.  
  80. without looking at the implementation, tell me what this thing does. Explain in a sentence what `children` means?
  81.  
  82. ```jsx
  83. const ContentFrame = ({title, HeaderComponent, children}) => {
  84. const Header = HeaderComponent || ContentFrameHeader;
  85.  
  86. return (
  87. <div className="content-frame">
  88. <Header>
  89. <Title>{ title }</Title>
  90. </Header>
  91. <ContentFrameBody>
  92. { children }
  93. </ContentFrameBody>
  94. </div>
  95. );
  96. }
  97. ```
  98.  
  99. usages:
  100. ```jsx
  101. <ContentFrame title="hello">
  102. I am body content
  103. </ContentFrame>
  104. ```
  105.  
  106. ```jsx
  107. <ContentFrame title="hello" HeaderComponent={CustomHeader}>
  108. I am body content
  109. </ContentFrame>
  110. ```
  111.  
  112. jsx props:
  113. ```jsx
  114. const ContentFrame = ({title, headerRightContent, children}) => {
  115. return (
  116. <div className="content-frame">
  117. <ContentFrameHeader>
  118. <Title>{ title }</Title>
  119. <ContentFrameHeaderRight>
  120. { headerRightContent }
  121. </ContentFrameHeaderRight>
  122. </ContentFrameHeader>
  123. <ContentFrameBody>
  124. { children }
  125. </ContentFrameBody>
  126. </div>
  127. );
  128. }
  129. ```
  130.  
  131. passing props:
  132. ```jsx
  133. const AcceptancePage = ({onSubmit}) => {
  134. return (
  135. <ContentFrame {...{
  136. title: 'Warning!',
  137. headerRightContent: (
  138. <SubmitButton onClick={onSubmit}>OKAY DOKAY</SubmitButton>
  139. );
  140. }}>
  141. Are you sure this is ok
  142. </ContentFrame>
  143. );
  144. };
  145. ```
  146.  
  147. ```jsx
  148. const AcceptancePageHeader = ({onSubmit}) => {
  149. return (
  150. <ContentFrameHeader>
  151. <Title> Warning! </Title>
  152. <ContentFrameHeaderRight>
  153. <SubmitButton onClick={onSubmit}> OKAY DOKAY </SubmitButton>
  154. </ContentFrameHeaderRight>
  155. </ContentFrameHeader>
  156. )
  157. };
  158.  
  159. const AcceptancePage = ({onSubmit}) => {
  160. return (
  161. <ContentFrame {...{
  162. HeaderComponent: AcceptancePageHeader,
  163. onSubmit,
  164. }}>
  165. Are you sure this is ok
  166. </ContentFrame>
  167. );
  168. };
  169. ```
Add Comment
Please, Sign In to add comment