简体   繁体   English

React Native - 如何使用相同的代码在 react-native 的两个不同页面中获得相同的输出?

[英]React Native - How to get same output in two different pages in react-native with same code?

(React native ) Here is my code for one screen(DetailsScreen) and I want to make other screen(IngredientScreen) with same output how to do that. (反应原生)这是我的一个屏幕(DetailsS​​creen)的代码,我想用相同的输出制作另一个屏幕(IngredientScreen)如何做到这一点。 when I tried to copy the same code in IngredientScreen I am getting some error.当我尝试在 IngredientScreen 中复制相同的代码时,出现了一些错误。 what kinds of modification do I need in DetailsScreen and what kinds of code need to be added in IngredientScreen.在DetailsS​​creen中需要做什么样的修改,在IngredientScreen中需要添加什么样的代码。 page need to be navigate from DetailsScreen to IngredientScreen.页面需要从 DetailsS​​creen 导航到 IngredientScreen。 (react native ) (反应原生)

Codes are here :-代码在这里:-

import React from "react";
import { SafeAreaView, StyleSheet, View, Text, Image } from "react-native";
import { ScrollView, TouchableOpacity } from "react-native-gesture-handler";
import Icon from "react-native-vector-icons/MaterialIcons";
import COLORS from "../../consts/colors";
import foods from "../../consts/foods";
import { SecondaryButton } from "../components/Button";

const DetailsScreen = ({ navigation, route }) => {
  const item = route.params;

  return (
    <SafeAreaView style={{ backgroundColor: COLORS.white }}>
      <View style={style.header}>
        <Icon name="arrow-back-ios" size={28} onPress={navigation.goBack} />
        <Text style={{ fontSize: 20, fontWeight: "bold" }}>Details</Text>
      </View>
      <ScrollView showsVerticalScrollIndicator={false}>
        <View
          style={{
            justifyContent: "center",
            alignItems: "center",
            height: 280,
          }}
        >
          <Image source={item.image} style={{ height: 220, width: 220 }} />
        </View>
        <View style={style.details}>
          <View
            style={{
              flexDirection: "row",
              justifyContent: "space-between",
              alignItems: "center",
            }}
          >
            <Text
              style={{ fontSize: 25, fontWeight: "bold", color: COLORS.white }}
            >
              {item.name}
            </Text>
          </View>
          <Text style={style.detailsText}>{item.details}</Text>
          

          <View style={{ marginTop: 40, marginBottom: 40, color: "red" }}>
            <TouchableOpacity
              onPress={() => navigation.navigate("IngredientScreen")}
            >
              <SecondaryButton title="Ingredients..." />
            </TouchableOpacity>
          </View>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};

const style = StyleSheet.create({
  header: {
    paddingVertical: 20,
    flexDirection: "row",
    alignItems: "center",
    marginHorizontal: 20,
  },
  details: {
    paddingHorizontal: 20,
    paddingTop: 40,
    paddingBottom: 60,
    backgroundColor: COLORS.primary,
    borderTopRightRadius: 40,
    borderTopLeftRadius: 40,
  },
  iconContainer: {
    backgroundColor: COLORS.white,
    height: 50,
    width: 50,
    justifyContent: "center",
    alignItems: "center",
    borderRadius: 30,
  },
  detailsText: {
    marginTop: 10,
    lineHeight: 22,
    fontSize: 16,
    color: COLORS.white,
  },
});

export default DetailsScreen;

You could create a new component and then import it in both screens.您可以创建一个新组件,然后将其导入到两个屏幕中。 For example:例如:

const DetailScreen = () => {
     return <ReusableComponent />
}

 const Ingredientcreen = () => {
     return <ReusableComponent />
}  


export const ReusableComponent = () => {
    return (
    <SafeAreaView style={{ backgroundColor: COLORS.white }}>
      <View style={style.header}>
        <Icon name="arrow-back-ios" size={28} onPress={navigation.goBack} />
        <Text style={{ fontSize: 20, fontWeight: "bold" }}>Details</Text>
      </View>
      <ScrollView showsVerticalScrollIndicator={false}>
        <View
          style={{
            justifyContent: "center",
            alignItems: "center",
            height: 280,
          }}
        >
          <Image source={item.image} style={{ height: 220, width: 220 }} />
        </View>
        <View style={style.details}>
          <View
            style={{
              flexDirection: "row",
              justifyContent: "space-between",
              alignItems: "center",
            }}
          >
            <Text
              style={{ fontSize: 25, fontWeight: "bold", color: COLORS.white }}
            >
              {item.name}
            </Text>
          </View>
          <Text style={style.detailsText}>{item.details}</Text>
          

          <View style={{ marginTop: 40, marginBottom: 40, color: "red" }}>
            <TouchableOpacity
              onPress={() => navigation.navigate("IngredientScreen")}
            >
              <SecondaryButton title="Ingredients..." />
            </TouchableOpacity>
          </View>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM