简体   繁体   English

本地反应:如何重构功能

[英]react native : how to refactor a function

I have multiple functions that looks almost identical. 我有多个功能几乎相同。 How can I refactor my function so that I don't have write multiple functions. 我该如何重构我的函数,以便不必编写多个函数。

This is my code: 这是我的代码:

renderImage = () => {
    var imgSource = this.state.image? imageOne : imageTwo;
    return (
      <View style={{marginLeft:20, marginTop:20,width:50, height:67, backgroundColor:'transparent', alignItems:'center'}}>
      <Image
        style={{width:46, height:46}}
        source={ imgSource }
      />
      <Text style={{paddingTop:4, fontSize:11, color:'#4a4a4a'}}>some text</Text>
      </View>
    );
  }

I have another function that looks very similar: 我还有另一个看起来非常相似的功能:

renderImage2 = () => {
    var imgSource = this.state.image2? imageThree : imageFour;
    return (
      <View style={{marginLeft:20, marginTop:20,width:50, height:67, backgroundColor:'transparent', alignItems:'center'}}>
      <Image
        style={{width:46, height:46}}
//I want to change source 
        source={ imgSource }
      />
//Also I want to change text
      <Text style={{paddingTop:4, fontSize:11, color:'#4a4a4a'}}>some other text</Text>
      </View>
    );
  }

I just want to change the Image source and the text . 我只想更改Image sourcetext Can I do this? 我可以这样做吗?

Create another component that returns the render but takes 2 props you pass in (source, text) 创建另一个返回渲染但需要传入2个道具的组件(源,文本)

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

class ImageWithText extends React.PureComponent {
  render() {
    const { source, text } = this.props;

    return (
      <View style={{ marginLeft: 20, marginTop: 20, width: 50, height: 67, backgroundColor: 'transparent', alignItems: 'center' }}>
        <Image style={{ width: 46, height: 46 }} source={source} />
        <Text style={{ paddingTop: 4, fontSize: 11, color: '#4a4a4a' }}>{text}</Text>
      </View>
    );
  }
}

export default ImageWithText;

and when you want to use the new component 以及何时要使用新组件

renderImage = () => {
  var imgSource = this.state.image? imageOne : imageTwo;
  return (
    <ImageWithText source={imgSource} text="some text">
  );
}

You can first define a renderImage function that takes in a parameter to do decision making. 您可以首先定义一个renderImage函数,该函数接受一个参数来进行决策。 Within this renderImage function, define all the possible images to load within a lookup object, such as below 在此renderImage函数中,定义要在lookup对象中加载的所有可能的图像,如下所示

renderImage = (renderImage) => {
  const lookup = {
    image_1: { src: imageOne, text: 'text_for_image_one' },
    image_2: { src: imageTwo, text: 'text_for_image_two' }
  }

  const selectedImage = lookup[renderImage] || undefined;

  if(!selectedImage) return;

  return (
      <View style={{marginLeft:20, marginTop:20,width:50, height:67, backgroundColor:'transparent', alignItems:'center'}}>
      <Image
        style={{width:46, height:46}}
        source={selectedImage.src}
      />
      <Text style={{paddingTop:4, fontSize:11, color:'#4a4a4a'}}>{selectedImage.text}</Text>
      </View>
    );
  }

Then within your render method do below 然后在您的render方法中执行以下操作

render() {
    ...
    {this.renderImage(image_1)}
    {this.renderImage(image_2)}
    ...
  }

You can define simple function render like 您可以定义简单的函数渲染,例如

renderImage = (imageSrc, text) => (
 <View style={{marginLeft:20, marginTop:20,width:50, height:67, backgroundColor:'transparent', alignItems:'center'}}>
      <Image
        style={{width:46, height:46}}
//I want to change source 
        source={ imageSrc }
      />
//Also I want to change text
      <Text style={{paddingTop:4, fontSize:11, color:'#4a4a4a'}}>{text}</Text>
      </View>
)

end use in your render like: 在渲染中的最终用途如下:

{this.renderImage(this.state.image? imageOne : imageTwo, 'some text')}

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

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