【问题标题】:React-native flex height反应原生的弹性高度
【发布时间】:2017-04-02 10:07:14
【问题描述】:

我有 3 个组件,我正在尝试设置它们的高度(页眉、正文、页脚),但是当它呈现时,每个组件只有它包含的一行文本的高度。

import React from 'react';
import { View, Text } from 'react-native';
import Header from './Header';
import Body from './Body';
import Footer from './Footer';
import * as globalStyles from '../styles/global';


const HomeScreen = () => (
  <View style={{ flex: 1 }}>
    <Header style={{ flex: 1 }} title={'MyTitle'}>
      <Text>Component</Text>
    </Header>
    <Body style={{ flex: 3 }}>
      <Text>my body</Text>
    </Body>
    <Footer style={{ flex: 1 }}>
      <Text>my footer</Text>
    </Footer>
  </View>
);

export default HomeScreen;

应用程序

import React from 'react';
import { Provider } from 'react-redux';
import HomeScreen from './components/HomeScreen';
import createStore from './createStore';

const store = createStore();

export default () => (
  <Provider store={store}>
    <HomeScreen />
  </Provider>
);

标题

import React, { PropTypes } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as globalStyles from '../styles/global';

const Header = ({ children, title }) => (
  <View>
    <Text style={globalStyles.COMMON_STYLES.text}>
      {children}
      {title}
      {title}
    </Text>
  </View>
);

Header.propTypes = {
  children: PropTypes.node
};

const styles = StyleSheet.create({
  header: {
    paddingTop: 20
  }
});
export default Header;

页脚

import React, { PropTypes } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as globalStyles from '../styles/global';

const Footer = ({ children }) => (
  <View>
    <Text style={globalStyles.COMMON_STYLES.text}>
      {children}
    </Text>
  </View>
);

Footer.propTypes = {
  children: PropTypes.node.isRequired
};

const styles = StyleSheet.create({
  header: {
    marginTop: 20,
    flex: 1,
  }
});
export default Footer;

身体

import React, { PropTypes } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import * as globalStyles from '../styles/global';

const Body = ({ children, title }) => (
  <View>
    <Text style={globalStyles.COMMON_STYLES.text}>
      {children}
      {title}
      {title}
    </Text>
  </View>
);

Body.propTypes = {
  children: PropTypes.node
};

const styles = StyleSheet.create({
  header: {
    marginTop: 20,
    flex: 1,
  }
});
export default Body;

全局样式

import { StyleSheet } from 'react-native';

export const BG_COLOR = '#343336';
export const BAR_COLOR = '#4e4d52';
export const TEXT_COLOR = '#e5dbda';
export const HEADER_TEXT_COLOR = '#fff';
export const MUTED_COLOR = '#8e8786';
export const LINK_COLOR = '#48e9d9';
export const ACCENT_COLORS = ['#d31d65', '#751c53', '#c248c0', '#7d6e8b', '#bbc6f7'];

export const COMMON_STYLES = StyleSheet.create({
  pageContainer: {
    backgroundColor: BG_COLOR,
    marginTop: 0,
    paddingTop: 5,
    paddingHorizontal: 10
  },
  text: {
    color: TEXT_COLOR,
    fontFamily: 'Helvetica Neue'
  }
});

我的理解是 Body 应该是页眉或页脚高度的 3 倍。

反应原生:

react-native-cli: 2.0.1
react-native: 0.41.2

【问题讨论】:

  • 仅此代码就可以按您的预期正常工作。尝试共享该屏幕的整个代码。顺便说一句,您使用的是哪个版本的 RN?
  • 我添加了更多代码 - 尽管那里有更多样式 - 我看不出它会如何影响高度。
  • 我要找的是你使用 HomeScreen 的地方,它是否被包裹在其他容器中,是否为那些包裹容器定义了 flex(如果有)?
  • 感谢您的回复 - 我不这么认为。请参阅我的更新答案。
  • Flex 应该被赋予页眉、正文和页脚组件的根容器。 Header、Footer 和 Body 是自定义组件,样式 props 不适用于它们,您应用的样式只是作为 prop 传递给子组件。

标签: react-native flexbox


【解决方案1】:

编辑 Flex 应该被赋予页眉、正文和页脚组件的根容器。 Header、Footer 和 Body 是自定义组件,样式 props 不适用于它们,您应用的样式只是作为 prop

传递
const Footer = ({ children }) => (
  <View style={{ flex: 1 }}>  // root container of footer
    <Text style={globalStyles.COMMON_STYLES.text}>
      {children}
    </Text>
  </View>
);

const Body = ({ children, title }) => (
   <View style={{ flex: 3 }}>
    <Text style={globalStyles.COMMON_STYLES.text}>
      {children}
      {title}
      {title}
    </Text>
   </View>
);

const Header = ({ children, title }) => (
  <View style={{ flex: 1 }}> // root container of header
    <Text style={globalStyles.COMMON_STYLES.text}>
      {children}
      {title}
      {title}
    </Text>
  </View>
);

希望它对你有用

【讨论】:

  • 好的。抱歉 - 我需要澄清一下。 Flex 应该被赋予根组件。你的意思是: ); ?我也不确定我是否理解你的最后一句话。
  • 通过 Header/Footer/Body 的根容器,我的意思是将 flex 赋予组件的最外层容器,即 View,如我的答案所示。 Styleprops 只能直接赋予原生组件。我将编辑我的答案以在代码中添加 cmets
  • HomeScreen 不是原生组件,因此无需直接赋予它 flex。因此,当您对其应用 style={{ flex: 1}} 时,它将被视为道具,并且在 HomeScreen 中,当您控制台 this.props.style 时,您将获得一个对象 { flex: 1 }。就像您将标题传递给 Header 组件一样。希望这可以消除您的疑问
  • +1 用于指定自定义组件默认不会设置样式,需要编写代码来使用样式,例如&lt;Header style={props.style} /&gt; -- Header, Footer and Body are custom components, the style props won't be applicable to them
猜你喜欢
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多