react组件目录组织_这些天来组织React组件(2018年上半年)

react组件目录组织

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.

自从我的React书是针对最近的历史(第14号React)编写以来,我开始通过@alexharrisonsax输入问题的答案,并且情况发生了变化。 Twitter不适用于代码讨论,因此请按此处。

导入依赖 (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:

如果使用Flow,一个好主意是定义属性和状态的类型,例如:

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 on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/organizing-react-component-h1-2018/

react组件目录组织

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值