简体   繁体   English

在 React Native 中将外部文件连接到 App.js 时遇到问题

[英]Trouble connecting external files to App.js in react native

I'm super new to react native & having a hard time connecting separate files with my App.js.我是本机反应的超级新手,并且很难将单独的文件与我的 App.js 连接起来。 I've got a user-defined component in a separate file.我在一个单独的文件中有一个用户定义的组件。 This is what I've right now in the app.js这就是我现在在 app.js 中的内容

import React from 'react';
import TextImg from "./components/comp";
import { StyleSheet, Text, View, Image } from 'react-native';

  export default function App() {
    return (
        <View style={{alignItems:'center', top:150}}>
          <Image source={require('./images/pic.jpeg')} style={{ width: 290, height: 190 }}/>
          <TextImg text='Mt. Fuji'/>
          <TextImg source={{imageUri: 'https://reactjs.org/logo-og.png'}} style={{width: 400, height: 400}} />
        </View> );
};

I am importing components from comp.js, and this is what I've in the comp.js file我正在从 comp.js 导入组件,这就是我在 comp.js 文件中的内容

import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
function TextImg(textprop, imgprop) {
    return (
        <React.Fragment>
          <Text>{textprop.text}</Text>
          <Image source={imgprop.imageUri}></Image>
        </React.Fragment>
    );
};

But this only shows the text, but not the image.但这仅显示文本,而不显示图像。 Can anyone help with this?有人能帮忙吗? Thanks in advance for help!预先感谢您的帮助!

All your props would be passed as a single parameter.您的所有道具都将作为单个参数传递。 So you should change your component like below.所以你应该像下面一样改变你的组件。

import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';

export default function TextImg(props) {
    return (
        <React.Fragment>
          <Text>{props.text}</Text>
          <Image source={props.imageUri} style={props.style}></Image>
        </React.Fragment>
    );
};

You will have to import it in your app.js.您必须在 app.js 中导入它。 (Assuming your file name is TextImg.js and its in the same path) (假设您的文件名是 TextImg.js 并且它在同一路径中)

import TextImg from './TextImg'

You are not passing your props correctly.您没有正确传递道具。

Here is a revamp of your code, but in the proper way:这是您的代码的改造,但以正确的方式:

import React from 'react';
import { TextImg } from "./components/comp";
import { StyleSheet, Text, View, Image } from 'react-native';

  export default function App() {
    return (
        <View style={{alignItems:'center', top:150}}>
          <Image source={require('./images/pic.jpeg')} style={{ width: 290, height: 190 }}/>
          <TextImg text='Mt. Fuji' imageUri: {'https://reactjs.org/logo-og.png'} />
        </View> );
};

Then in comp.js然后在 comp.js

import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';
export const TextImg = ({ text, imageUri }) => {
    return (
        <React.Fragment>
          <Text>{text}</Text>
          <Image source={imageUri} style={{width: 400, height: 400}} />
        </React.Fragment>
    );
};

This will fix it up for you.这将为您解决问题。 Study the code and try to understand what was done.研究代码并尝试理解做了什么。 But I'll also advise you to learn about React props.但我也会建议你学习 React props。

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

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