javascript - 如何在 native react 中将按钮从一个类导入到另一个类

标签 javascript react-native

按钮.js

import { View, Text, TouchableOpacity, Button} from 'react-native';
import React from 'react';

export default function Btn (bgColor, textColor, btnLabel) {
  return (
    <TouchableOpacity
      style={{
        backgroundColor: bgColor,
        borderRadius: 100,
        alignItems: 'center',
        width: 250
      }}
    >
      <Text style={{ color: textColor, fontSize: 22, fontWeight: "bold" }}>
        {btnLabel}
      </Text>
    </TouchableOpacity>
  );
}

Home.js

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Background from './Background';
import Btn from './Btn';
import { darkGreen } from './Constants';
// create a component
export default function Home (){
    return (
        <Background>
            <View style= {{ marginHorizontal: 40, marginVertical: 100}}>
           <Btn bgColor={darkGreen} textColor='white' btnLabel="Login"/>
            </View>
            
        </Background>
        
    );
};

// define your styles
const styles = StyleSheet.create({});

最佳答案

方法一

您可以简单地调用 Button 组件作为函数,如下所示

<Background>
  <View style= {{ marginHorizontal: 40, marginVertical: 100}}>
    {Btn(darkGreen,'white',"Login")}
  </View>
</Background> 

方法2

您可以像下面这样初始化 Button 组件

export default function Btn ({bgColor, textColor, btnLabel}) {
   ...
}

注意:当你不像函数一样调用组件时,你需要像上面那样解构 props

关于javascript - 如何在 native react 中将按钮从一个类导入到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74297988/

相关文章:

javascript - 使用 Switch 语句在 React 中预期的表达式

javascript - Kendo - 选择网格单元格时更新另一个网格(mvvm)

javascript函数在变量中的函数

javascript - 无法在angularJs中设置下拉列表的选定值

react-native - React Native 支持 Display P3 色域?还是只有srgb?

javascript - 如何使用正则表达式获取字符串的特定部分并将其放入平面列表中(react-native)

javascript - 调用请求后如何重新渲染屏幕?

react-native - 创建导航器不带参数

javascript - Ajax回调函数的位置

javascript - Ruby/Sinatra/jQuery 应用程序未在 repl.it 中运行