javascript - How to render React components, stored in an array, inside the render method of another component - Stack Overflow

I've got a ponent, let's call Screen, which imports a ponent that will contain a search resul

I've got a ponent, let's call Screen, which imports a ponent that will contain a search result. A method in Screen loops through an array of objects and for each object instaniates a new search result ponent and stores them in an array.

I'm new to React, so my question is, is it correct to create an instance of a class extending Component using the new keyword, or is there a proper React way to do this? If so what is the correct way of rendering these ponents in the Screen class, as the way I'm doing it in the snippet below doesn't seem to work as nothing renders.

Screen.js - I've cut down the file to just show what I'm on about

import React, { Component } from "react";
import { Text, View } from "react-native";

import SearchResult from "ponents/SearchResult";


class Screen extends Component {
    searchResultsComponents = [];

    submitSearchQuery(searchParam /* Array of objects */) {
        this.searchResultsComponents = [];
        for(let result in this.searchResults) {
            this.searchResultsComponents.push(new SearchResult(this.searchResults[result]));
            }
            this.forceUpdate();
        });
    }

    render() {
        return(
            <View>  
                { this.searchResultsComponents.forEach((it) => { it.render(); }) }
            </View>
        );
    }
}

export default Screen;

SearchResult.js

import React, { Component } from "react";
import { Text, View } from "react-native";


class SearchResult extends Component {
    title;
    setTitle(title) {
        this.title = title;
    }
    getTitle() {
        return this.title;
    }

    description;
    setDescription(description) {
        this.description = description;
    }
    getDescription() {
        return this.description;
    }

    constructor(searchResult) {
        super();
        this.setTitle(searchResult.snippet.title);
        this.setDescription(searchResult.snippet.description)
    }

    render() {
        return(
            <View>
                <Text>{ this.title }</Text>
                <Text>{ this.description }</Text>
            </View>
        );
    }
}

export default SearchResult;

I've got a ponent, let's call Screen, which imports a ponent that will contain a search result. A method in Screen loops through an array of objects and for each object instaniates a new search result ponent and stores them in an array.

I'm new to React, so my question is, is it correct to create an instance of a class extending Component using the new keyword, or is there a proper React way to do this? If so what is the correct way of rendering these ponents in the Screen class, as the way I'm doing it in the snippet below doesn't seem to work as nothing renders.

Screen.js - I've cut down the file to just show what I'm on about

import React, { Component } from "react";
import { Text, View } from "react-native";

import SearchResult from "ponents/SearchResult";


class Screen extends Component {
    searchResultsComponents = [];

    submitSearchQuery(searchParam /* Array of objects */) {
        this.searchResultsComponents = [];
        for(let result in this.searchResults) {
            this.searchResultsComponents.push(new SearchResult(this.searchResults[result]));
            }
            this.forceUpdate();
        });
    }

    render() {
        return(
            <View>  
                { this.searchResultsComponents.forEach((it) => { it.render(); }) }
            </View>
        );
    }
}

export default Screen;

SearchResult.js

import React, { Component } from "react";
import { Text, View } from "react-native";


class SearchResult extends Component {
    title;
    setTitle(title) {
        this.title = title;
    }
    getTitle() {
        return this.title;
    }

    description;
    setDescription(description) {
        this.description = description;
    }
    getDescription() {
        return this.description;
    }

    constructor(searchResult) {
        super();
        this.setTitle(searchResult.snippet.title);
        this.setDescription(searchResult.snippet.description)
    }

    render() {
        return(
            <View>
                <Text>{ this.title }</Text>
                <Text>{ this.description }</Text>
            </View>
        );
    }
}

export default SearchResult;
Share Improve this question asked Jun 12, 2018 at 21:24 Jordan GarveyJordan Garvey 3455 silver badges16 bronze badges 1
  • 1 dont call new on the ponent... just create the ponent instead this.searchResultsComponents.push( <SearchResult {...this.searchResults[result]} />) then in your render just render the array <View>{this.searchResultsComponents}</View> – John Ruddell Commented Jun 12, 2018 at 21:32
Add a ment  | 

2 Answers 2

Reset to default 2

The proper way to approach this is, inside your render method:

 { this.searchResultsComponents.map((it, index) => <it key={index} />) }

The key is optional, but remended by react to improve rendering performance when rendering an array of ponents.

See https://reactjs/docs/lists-and-keys.html#basic-list-ponent for more details.

Your approach does not work, because by just calling the render method, the HTML DOM is returned by the function call. However, you just call the function and don't do anything with the return value.

The reason your approach doesn't work is that forEach method doesn't return anything. Instead of forEach use map. Your modified line should look something like this:

this.searchResultsComponents.map((ponent, index) => <View key={index}>{ponent}</View>);

The reason I wrapped the ponent into View is because this way each item can get key attribute which is remended when displaying arrays.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信