CLooker

Untitled

Apr 16th, 2018
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is what I was trying to ask you last night about how ts plays with arg destructuring
  2.  
  3. import React, { Component } from 'react';
  4.  
  5. // destructuring args in fn definition
  6. const DisplayName = ({ name, country }) => <p>{name} lives in {country}</p>
  7.  
  8. export default class App extends Component {
  9.     render() {
  10.         return (
  11.             <DisplayName
  12.                 name={'Josh'}
  13.                 country={'USA'}
  14.             />
  15.     }
  16. }
  17.  
  18. // maybe ts wants this?
  19. const DisplayName = ({ name: string, country: string }): string => <p>{name} lives in {country}</p>
  20.  
  21. // But in normal js that's equivalent to renaming the `name` prop on `props` to `string`.
  22. // As well as doing the same for `country` (which could throw an error as is, re-assigning to same variable name).
  23. // For instance, in normal js, with what is above, you're saying "Ok I'm grabbing the `name` prop off the object that is
  24. // passed into `DisplayName`, but then I'm renaming it `string`, so in the fn body, I can start using `string`
  25. // and it will refer to whatever the value of `name` is.
  26.  
  27. // Here's the answer
  28. const DisplayName = ({ name, country }: { name: string, country: string}): string => <p>{name} lives in {country}</p>
  29.  
  30. // YAY
Add Comment
Please, Sign In to add comment