javascript - calling functions that return a JSX element in render function - Stack Overflow

I am trying to reduce the length of the render()method to improve readability by calling class methods

I am trying to reduce the length of the render()method to improve readability by calling class methods that contains isolated JSX elements.

The problem is that this technique won't work for more than one JSX element.

I embedded each element inside a <View> but it doesn't prevent this error

Invariant Violation : Text strings must be rendered within a <Text> ponent.
import React, { Component } from 'react';
import { Text, View,StyleSheet } from 'react-native';

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center"
    }})

export default class HelloWorldApp extends Component {
    header() {
        return  (
            <Text>header</Text>
        );

    }
    firstElement() {
        return (
                <Text>first element</Text>
        );

    }
    secondElement() {
        return (
            <Text>second element</Text>
        );

    }

    footer(){
        return (
            <Text>footer</Text>
        );
    }

    render() {

     let header  = this.header();
     let firstElement = this.firstElement();
     let secondElement= this.secondElement();
     let footer = this.footer();

        return (
            <View style={styles.container}>
                header,
                firstElement,
                secondElement,
                footer,
            </View>

        );

    }


}

I am aware that the syntax in render()is not correct, it is to show you what i want the code to look like.

I am trying to reduce the length of the render()method to improve readability by calling class methods that contains isolated JSX elements.

The problem is that this technique won't work for more than one JSX element.

I embedded each element inside a <View> but it doesn't prevent this error

Invariant Violation : Text strings must be rendered within a <Text> ponent.
import React, { Component } from 'react';
import { Text, View,StyleSheet } from 'react-native';

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center"
    }})

export default class HelloWorldApp extends Component {
    header() {
        return  (
            <Text>header</Text>
        );

    }
    firstElement() {
        return (
                <Text>first element</Text>
        );

    }
    secondElement() {
        return (
            <Text>second element</Text>
        );

    }

    footer(){
        return (
            <Text>footer</Text>
        );
    }

    render() {

     let header  = this.header();
     let firstElement = this.firstElement();
     let secondElement= this.secondElement();
     let footer = this.footer();

        return (
            <View style={styles.container}>
                header,
                firstElement,
                secondElement,
                footer,
            </View>

        );

    }


}

I am aware that the syntax in render()is not correct, it is to show you what i want the code to look like.

Share Improve this question asked Nov 13, 2019 at 23:33 2mk2mk 231 silver badge5 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

Your error states that: you're having string text outside of a <Text> ponent. In your code, your header texts and mas ( , ) are the problem. Here is how to fix it:

return (
    <View style={styles.container}>
        {header}
        {firstElement}
        {secondElement}
        {footer}
        // or you can directly use your functions
        /*
        {this.header()}
        {this.firstElement()}
        {this.secondElement()}
        {this.footer()}
        */
    </View>
);

P/s: In case you still don't understand why you got the error, you can try this:

return (
    <View style={styles.container}>
        <Text>
            header
            firstElement,
            secondElement,
            footer,
        </Text>
    </View>
);

The problem was in your return You forgot to wrap your functions in {braces}. See How to Call a Function inside a Render in React/Jsx for a better explanation.

Change it to:

    return (
      <View style={styles.container}>
        {header}
        {firstElement}
        {secondElement}
        {footer}
      </View>
    );
return (
    <View style={styles.container}>
         {header()}
         {firstElement()}
         {secondElement()}
         {footer()}
    </View>
);

but instead of doing that, you can just make their own ponents and import them.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745276249a4620060.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信