您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AspectRatioDetector.native.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { setAspectRatio } from '../actions';
  6. import styles from './styles';
  7. /**
  8. * A root {@link View} which captures the 'onLayout' event and figures out
  9. * the aspect ratio of the app.
  10. */
  11. class AspectRatioDetector extends Component {
  12. /**
  13. * AspectRatioDetector component's property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * The "onLayout" handler.
  20. */
  21. _onLayout: PropTypes.func,
  22. /**
  23. * Any nested components.
  24. */
  25. children: PropTypes.node
  26. };
  27. /**
  28. * Renders the root view and it's children.
  29. *
  30. * @returns {Component}
  31. */
  32. render() {
  33. return (
  34. <View
  35. onLayout = { this.props._onLayout }
  36. style = { styles.aspectRatioDetector } >
  37. { this.props.children }
  38. </View>
  39. );
  40. }
  41. }
  42. /**
  43. * Maps dispatching of the aspect ratio actions to React component props.
  44. *
  45. * @param {Function} dispatch - Redux action dispatcher.
  46. * @private
  47. * @returns {{
  48. * _onLayout: Function
  49. * }}
  50. */
  51. function _mapDispatchToProps(dispatch) {
  52. return {
  53. /**
  54. * Handles the "on layout" View's event and dispatches aspect ratio
  55. * changed action.
  56. *
  57. * @param {Object} event - The "on layout" event object/structure passed
  58. * by react-native.
  59. * @private
  60. * @returns {void}
  61. */
  62. _onLayout({ nativeEvent: { layout: { height, width } } }) {
  63. dispatch(setAspectRatio(width, height));
  64. }
  65. };
  66. }
  67. export default connect(undefined, _mapDispatchToProps)(AspectRatioDetector);