2017-09-16 95 views
0

我是React的新手,但不是JS。 这里有一段代码,我无法理解React属性/函数赋值/声明

// @flow 

import React, { Component } from 'react'; 
import ShowCard from './ShowCard'; 
import Header from './Header'; 

class Search extends Component { 
    state = { 
    searchTerm: '' 
    }; 
    props: { 
    shows: Array<Show> 
    }; 
    handleSearchTermChange = (event: SyntheticKeyboardEvent & {target: HTMLInputElement}) => { 
    this.setState({ searchTerm: event.target.value }); 
    }; 
    render() { 
    return (
     <div className="search"></div> 
    ); 
    } 
} 

export default Search; 

在类中有像state = ...props: {..表达式。 功能定义如render(){}handleSearchTermChange = (...。 正如我从所有这些有效的ES6记得一个是render。它是如何工作的?如果我切换数据分配到state/props的方式 - 一切都会刹车。我认为这是某种babel插件,是吗?

+0

这是JavaScript与[** Flow **](https://flow.org/)。 [** This **](https://pastebin.com/bKTq5TXQ)在JavaScript中是等效的,没有[** PropTypes **](https://facebook.github.io/react/docs/typechecking -with-proptypes.html)。 – Tholle

回答

0

是的,你是对的。

反应文档通常通过constructor() {}对象定义状态。但是,在某些项目中,我们可以通过state = {}来完成。由于Babel transpiler称为Class properties transform,这种简写语法是可能的。