StudyOptionsComponent在Web上显示,但不在iOS/移动的上显示- React Native

vecaoik1  于 2023-05-18  发布在  React
关注(0)|答案(1)|浏览(155)

我有一个叫做StudyOptionsComponent的组件,它非常基本,但由于某种原因,它不会在iOS上显示,但它会在Web上显示。不同平台之间唯一的变化是大小和flexDirection。所有其他组件都如预期的那样显示,这实际上是更简单的组件集之一。

App.tsx

import React from "react";
import { useState, useEffect } from "react";
import ...

const Title = ({ set }: { set: FlashcardsSet }) => {
  return ...
};

export default function App() {
  const [url, setUrl] = useState("");
  const [set, setData] = useState<FlashcardsSet>();
  const [isLoading, setIsLoading] = useState(false);
  const [loadingTime, setLoadingTime] = useState(0);

  ...

  return (
    <SafeAreaView style={styles.container}>
      ...
      <View>
        {set && (
          <View style={styles.setScreen}>
            {set && <Title set={set} />}
            {set && <SetScreen flashcardsSet={set} />}
          </View>
        )}
        {set && <StudyOptionsComponent set={set} />}
        <View style={{ position: "absolute", alignSelf: "center" }}>
          <Spinner
            visible={isLoading}
            textContent={
              loadingTime <= 5 ? "Loading..." : "This might take a while..."
            }
            textStyle={{ color: "white" }}
          />
        </View>
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#FAF9F6",
    alignItems: "center",
  },
  header: {
    flexDirection: "row",
    height: 80,
    width: "100%",
    backgroundColor: "#FFFDFA",
    alignItems: "center",
    justifyContent: Platform.OS == "web" ? "space-between" : "space-evenly",
    paddingHorizontal: 10,
    borderBottomWidth: 1,
    borderBottomColor: "#CCCCCC",
  },
  setScreen: {
    alignContent: "flex-start",
    alignSelf: "flex-start",
    padding: 25,
  },
  title: {
    width: "80%",
    fontSize: 25,
    fontWeight: "500",
  },
});

StudyOptionsComponent

import React from "react";
import { Platform, View } from "react-native";
import ...

const StudyOptionsComponent = ({ set }: { set: FlashcardsSet }) => {
  const studyComponentFlex = Platform.OS == "web" ? 1 / 5 : 1;
  const width = Platform.OS == "web" ? undefined : "90%";
  const studyComponentFlexDirection = Platform.OS == "web" ? "row" : "column";
  return (
    <View
      style={{
        flexDirection: studyComponentFlexDirection,
        width: "100%",
        justifyContent: "space-evenly",
      }}
    >
      <View style={{ flex: studyComponentFlex, width: width }}>
        <StudyComponent flashcardsSet={set} studyOption="learn" />
      </View>
      <View style={{ flex: studyComponentFlex, width: width }}>
        <StudyComponent flashcardsSet={set} studyOption="study" />
      </View>
      <View style={{ flex: studyComponentFlex, width: width }}>
        <StudyComponent flashcardsSet={set} studyOption="match" />
      </View>
      <View style={{ flex: studyComponentFlex, width: width }}>
        <StudyComponent flashcardsSet={set} studyOption="test" />
      </View>
    </View>
  );
};

export default StudyOptionsComponent;

StudyComponent

import { StatusBar } from "expo-status-bar";
import React, { useState } from "react";
import ...

type Props = {
  flashcardsSet: FlashcardsSet;
  studyOption: StudyOption;
};

const StudyComponent: React.FC<Props> = ({ flashcardsSet, studyOption }) => {
  return (
    <TouchableOpacity style={styles.container}>
      <Text style={{ fontSize: 15, fontWeight: "400" }}>
        {studyOption.charAt(0).toLocaleUpperCase() + studyOption.slice(1)}
      </Text>
    </TouchableOpacity>
  );
};

export default StudyComponent;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    top: 5,
    alignItems: "center",
    aspectRatio: 2 / 0.8,
    borderRadius: 15,
    borderColor: "grey",
    borderWidth: 1,
    elevation: 15,
    justifyContent: "center",
    backgroundColor: "#FFFDFA",
    shadowColor: "#000",
    shadowOffset: {
      width: 0,
      height: 0,
    },
    shadowOpacity: 0.25,
    shadowRadius: 2,
  },
});
yeotifhr

yeotifhr1#

我需要用ScrollView而不是View Package 集合组件才能看到这个问题。StudyOptionsComponent远远低于SetScreen,如果不滚动我就看不到了。下面是固定的工作代码:

{set && (
    <View style={styles.setScreen}>
      {set && <Title set={set} />}
      {set && <SetScreen flashcardsSet={set} />}
      {set && <View style={{marginTop: 15}}><StudyOptionsComponent set={set} /></View>}
    </View>
  )}

相关问题