react常用API总结
其实react涉及的内容并不是很多,把react API整理了一下,版本为0.14.7。
React相关 创建组件
1 2 3 4 5 6 7 8 9 React.createClass ({}) class ExampleComponent extends React .Component () {}var HelloMsg => function (props ) { return <div > Hello {props.name}</div > }
ReactElement
1 2 3 4 5 6 7 8 React.createElement(HTMLTag|ReactClass, {props}?, [children...]?) React.cloneElement(ReactElement, {props}?, [children...]?) React.isValidElement(ReactElement)
ReactDOM相关 1 2 3 4 5 6 7 8 9 ReactDOM.render (ReactElement, DOMElement, callback?) ReactDOM.findDOMNode(ReactComponent) ReactDOM.unmountComponentAtNode(DOMElement)
服务器渲染ReactDOMServer 1 2 3 4 ReactDOMServer.renderToString (ReactElement) ReactDOMServer.renderToStaticMarkup (ReactElement)
组件Component API 1 2 3 4 5 6 7 8 9 10 11 12 setState ( FunctiOn *|{ nextState } , callBacK? ) forceUpdate ( callBacK? ) render (){} constructor ( props ) { super (props); this .state = {...} }
组件Component事件 1 2 3 4 5 6 7 8 9 10 11 12 13 componentWillMount () componentDidMount () componentWillReceiveProps ( {nextProps } ) shouldComponentUpdate ( {nextProps } , {nextState } ) componentWillUpdate ( {nextProps } , {nextState } componentDidUpdate ( {previousProps } , {previousState } ) componentWillUnmount ()
Tag相关 1 2 3 4 5 6 key < ExampleComponent key = "uniqueValue" /> ref < ExampleComponent ref ={ S tRing |c allBacK } /> dangerouslySetInnerHTML < span dangerouslySetInnerHTML ={ __Html : String } />
Props相关 1 2 3 4 5 6 7 this .props.children < Component >{ this .props.children }</ Component >... < ExampleComponent { ... this .props} /> ReactComponentClass.defaultProps = DefaultPropertiesObject ReactComponentClass.propTypes = PropertiesSpecificationObject
propTypes相关 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 React.PropTypes.array React.PropTypes.bool React.PropTypes.func React.PropTypes.number React.PropTypes.object React.PropTypes.string React.PropTypes.node (anything that can be rendered]) React.PropTypes.element (ReactElement) React.PropTypes.instanceOf(Message) (must be of javascript type) React.PropTypes.oneOf(['News' , 'Photos' ]) (specify enumerated values) React.PropTypes.oneOfType([React.PropTypes.string, React.PropTypes.number]) (limit property types) React.PropTypes.arrayOf(React.PropTypes.number) (limit to a typed array) React.PropTypes.objectOf(React.PropTypes.number) (limit to a typed object) React.PropTypes.shape({color : React.PropTypes.string, fontSize : React.PropTypes.number}) (limit to object with specific keys/types) React.PropTypes.func.isRequired (produce an error if the property isn't passed to the child) React.PropTypes.any.isRequired (can be any object but must be required) (props, propName, componentName) => Boolean (create a custom property with the following function signature)