首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导出/呈现结构以使用组件react native

导出/呈现结构以使用组件react native
EN

Stack Overflow用户
提问于 2020-12-08 18:38:49
回答 1查看 12关注 0票数 0

我想要这个代码作为一个组件,而不是它加载我在一开始,简单地导出它,以使用它在另一个屏幕上,不是主页,我正在使用expo作为框架

我想要这个代码作为一个组件,而不是它加载我在一开始,简单地导出它,以使用它在另一个屏幕上,不是主页,我正在使用expo作为框架

代码语言:javascript
复制
import React from 'react';
import { StyleSheet, Text, ScrollView, View, TouchableWithoutFeedback } from 'react-native';

import { RadioButtons, SegmentedControls } from 'react-native-radio-buttons';

export default class App extends React.Component {

  state = {}

  render() {
    return (
      <View>
        {this.renderSegmentControlClone()}
      </View>
    );
  }

  // Super basic example
  
  renderSegmentControlClone(){
    const options = [
      'M',
      'F',
     
    ];

    function setSelectedOption(selectedSegment){
      this.setState({
        selectedSegment
      });
    }

    return (
      <View style={{ width: 335, }}>
        
        <SegmentedControls

tint= {'#C2E6E8'}
          
selectedTint= {'black'}
backTint= {'#FCFAF3'}
optionStyle= {{
  fontSize: 30,
  fontWeight: 'bold',
  fontFamily: 'semi-bold'
}}
containerStyle= {{
  marginLeft: 20,
  marginRight: 20,
}}
          options={ options }
          onSelection={ setSelectedOption.bind(this) }
          selectedOption={ this.state.selectedSegment }
        />
        <Text style={{marginTop: 10}}> {this.state.selectedSegment || 'none'}</Text>
      </View>);
  }
  }
EN

回答 1

Stack Overflow用户

发布于 2020-12-08 18:45:14

components文件夹中创建SegmentedControlComponent,如

代码语言:javascript
复制
// SegmentedControlComponent.js
export default class SegmentedControlComponent extends React.Component {
  state = {};

  render() {
    return <View>{this.renderSegmentControlClone()}</View>;
  }

  // Super basic example

  renderSegmentControlClone() {
    const options = ["M", "F"];

    function setSelectedOption(selectedSegment) {
      this.setState({
        selectedSegment,
      });
    }

    return (
      <View style={{ width: 335 }}>
        <SegmentedControls
          tint={"#C2E6E8"}
          selectedTint={"black"}
          backTint={"#FCFAF3"}
          optionStyle={{
            fontSize: 30,
            fontWeight: "bold",
            fontFamily: "semi-bold",
          }}
          containerStyle={{
            marginLeft: 20,
            marginRight: 20,
          }}
          options={options}
          onSelection={setSelectedOption.bind(this)}
          selectedOption={this.state.selectedSegment}
        />
        <Text style={{ marginTop: 10 }}>
          {" "}
          {this.state.selectedSegment || "none"}
        </Text>
      </View>
    );
  }
}

其他一些你想要使用它的屏幕,比如Example.js

代码语言:javascript
复制
import {SegmentedControlComponent} from "../components";

export default class Example extends React.Component {

  render() {
    return <SegmentedControlComponent />;
  }
}

在App.js中

代码语言:javascript
复制
export default class App extends React.Component {

  render() {
    return <View><Text>Home</Text></View>;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65197340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档