双色球走势图双彩网:Organizing a React component these days (H1/2018)

May 25th, 2018. Tagged: react

I started typing an answer to a question by @alexharrisonsax since my React book is written for the recent past (React 14) and things change. Twitter is not great for code discussions, so here goes.

Import dependencies

import React, {Component, Node} from 'react';

Declare the component

class App extends Component {}

Unless it's functional stateless component (preferably), in which case:

const App = ({name, description}) =>
  <div>
    <h1>{name}</h1>
    <h2>{description}</h2>
  </div>;

Flow

If using Flow, a good idea is to define the types of properties and state, like:

type Props = {
  name: string,
  description: string,
};

type State = {
  theTruth: boolean,
};

Then the class declaration becomes:

class App extends Component<Props, State> {
  state: State = { // property initializer for initial state
    theTruth: false,
  };

  // optional, only if the initial state is not good enough
  // or there are other things you need to do
  constructor(props: Props): void {
    super(props);
    this.state = { 
      theTruth: props.description === 'shall set you freeee',
    }; 
  }

  render(): Node {
    return <div>{/* fun with this.props and this.state */}</div>;
  }
}

Tell your friends about this post: Facebook, Twitter, Google+

Sorry, comments disabled and hidden due to excessive spam. Working on restoring the existing comments...

Meanwhile, hit me up on twitter @stoyanstefanov


  • “四新”彰显党的十九大思想灵魂和精髓要义 2018-08-17
  • 热身赛-U17女篮73-68立陶宛 杨舒宇19分刘禹彤13分 2018-08-16
  • 七月流火:一半是甜蜜 一半是仁慈 2018-08-15
  • “嫦娥”飞向月球背面,将会揭开哪些秘密? 2018-08-15
  • 一代枭雄身后事:“曹操墓”认定过程缘何一波三折? 2018-08-04
  • 女出纳侵吞千万公款扮富婆 7年未被公司发现 2018-08-04
  • 南昌PM2.5浓度写进目标责任书 切实防治大气污染 2018-07-20
  • 网友投诉不文明养犬行为 有关部门加强治理 2018-07-20
  • 767| 27| 409| 276| 713| 630| 406| 783| 431| 497|