admin管理员组文章数量:1023764
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 insteadthis.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
2 Answers
Reset to default 2The 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.
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 insteadthis.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
2 Answers
Reset to default 2The 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.
本文标签:
版权声明:本文标题:javascript - How to render React components, stored in an array, inside the render method of another component - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745598233a2158300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论