React Native { flex: 1 } 对于 React JS 的等价物是什么?

Smo*_*_js 6 javascript css flexbox reactjs react-native

我一直在用 React Native 编程,我正在尝试 React js(仅限 Web),但我不知道如何制作一个简单的视图,让所有屏幕开始与所有组件一起玩。

让我解释:

在 React Native 中,我使用 flex 处理视图维度,如下所示:

<View style={{ flex: 1 }}>
    <View style={{ flex: 0.5, backgroundColor: 'blue' }}>
      <Text>I'm a blue 50% screen View!</Text>
    </View>
    <View style={{ flex: 0.5, backgroundColor: 'yellow' }}>
      <Text>I'm a yellow 50% screen View!</Text>        
    </View>
</View>
Run Code Online (Sandbox Code Playgroud)

但是在 React JS 中,我必须使用无法识别 flex 的 div 标签。我的意思是,我不能这样做:

<div style={{ flex: 1 }}>
    <div style={{ flex: 0.5, backgroundColor: 'blue' }}>
      <p>I'm a blue 50% screen View!</p>
    </div>
    <div style={{ flex: 0.5, backgroundColor: 'yellow' }}>
      <p>I'm a yellow 50% screen View!</p>        
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我必须如何在 React js 中为 div 使用该样式以获得百分比结果?

far*_*ard 7

React Native 使用自己的 flexbox 实现 - https://facebook.github.io/react-native/docs/flexbox

在 React 中,您使用的是 CSS flexbox - https://developer.mozilla.org/en-US/docs/Glossary/Flexbox

Flexbox 在 React Native 中的工作方式与在 Web 上的 CSS 中的工作方式相同,但有一些例外。默认值不同,flexDirection 默认为 column 而不是 row,并且 flex 参数仅支持单个数字。

在 css 中,您还需要将父容器声明为

.flex-container {
  display: flex;
}
Run Code Online (Sandbox Code Playgroud)

这可能就是你所缺少的。

flex 的纯 CSS 版本可能看起来像这样(样式为“字符串”版本):

<div style="display: flex; flex-direction: column; height: 100vh;">
    <div style="flex: 1; background-color: blue;">
      <p>I'm a blue 50% screen View!</p>
    </div>
    <div style="flex: 1; background-color: yellow;">
      <p>I'm a yellow 50% screen View!</p>        
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)