React - this.props.appRecommendData.map is not a function












0















I am a beginner at ReactJS, I aim to make an app searching web application. Here is my idea:

In app.js, I have a state: appRecommendData which retrieve the result from the app store API and do searching by a function filterAppRecommend(), and then rendered by a component called AppRecommend, I found that I cannot render the result initially, but it works after typing, because of the async problem(when I still fetching the data, the export function have started), then I tried to add async and await to solve it, it works but the props.map is not function, can anyone help to solve it/suggest other way to fix it? below is my code:

app.js:



class App extends Component {
constructor(props) {
super(props);
this.state = {
filteredAppList: ,
filteredRecommend:filterAppRecommend("",10)
};
}
handleSearchChange = event =>{
this.setState({
filteredRecommend:filterAppRecommend(event.target.value,10)
});
}
render() {

return (
<div className="App">
<header className="App-header">
<div className="search-bar"><SearchInput textChange={this.handleSearchChange} /></div>
<div className="App-recommendation">
<AppRecommend appRecommendData={this.state.filteredRecommend}/>
</div>
</header>
</div>
);
}
}
}


InfilterAppRecommend.js:



export default async function filterAppRecommend(searchText, maxResults) {
console.log("maxResults is"+maxResults);
const api = await fetch("https://itunes.apple.com/hk/rss/topgrossingapplications/limit=10/json")
.then(results=>results.json());
console.log(api);
let datas = api.feed.entry;
console.log(datas);
return datas.filter(data => {
if(searchText===""){
return true;
}
if (data["im:name"].label.toLowerCase().includes(searchText.toLowerCase())) {
return true;
}
if (data["im:name"].label.includes(searchText)) {
return true;
}
return false;
})
.slice(0, maxResults);

}


In Component AppRecommend:



class AppRecommmend extends PureComponent{
constructor(props){
super(props);
this.state = {
isLoading:false,
};
}
render(){
console.log(this.props.appRecommendData.type); //will show undefined
return(
<div className="component-AppRecommend">
{this.props.appRecommendData.map(appRecommendData=>(
<AppRecommmendCol
imgSource={appRecommendData["im:image"][1].label}
title={appRecommendData["im:name"].label}/>
))}
</div>
);}
}
AppRecommmend.propTypes = {
appRecommendData: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array
])
}
export default AppRecommmend









share|improve this question

























  • Please try and format your code, as it's quite hard to read. filterAppRecommend is asynchronous, so you can't use x directly the way you do in the constructor of App.

    – Tholle
    Nov 15 '18 at 14:55











  • Formatted some of them, see if it is better. Thank you so much!

    – Eric Kwan
    Nov 15 '18 at 15:00


















0















I am a beginner at ReactJS, I aim to make an app searching web application. Here is my idea:

In app.js, I have a state: appRecommendData which retrieve the result from the app store API and do searching by a function filterAppRecommend(), and then rendered by a component called AppRecommend, I found that I cannot render the result initially, but it works after typing, because of the async problem(when I still fetching the data, the export function have started), then I tried to add async and await to solve it, it works but the props.map is not function, can anyone help to solve it/suggest other way to fix it? below is my code:

app.js:



class App extends Component {
constructor(props) {
super(props);
this.state = {
filteredAppList: ,
filteredRecommend:filterAppRecommend("",10)
};
}
handleSearchChange = event =>{
this.setState({
filteredRecommend:filterAppRecommend(event.target.value,10)
});
}
render() {

return (
<div className="App">
<header className="App-header">
<div className="search-bar"><SearchInput textChange={this.handleSearchChange} /></div>
<div className="App-recommendation">
<AppRecommend appRecommendData={this.state.filteredRecommend}/>
</div>
</header>
</div>
);
}
}
}


InfilterAppRecommend.js:



export default async function filterAppRecommend(searchText, maxResults) {
console.log("maxResults is"+maxResults);
const api = await fetch("https://itunes.apple.com/hk/rss/topgrossingapplications/limit=10/json")
.then(results=>results.json());
console.log(api);
let datas = api.feed.entry;
console.log(datas);
return datas.filter(data => {
if(searchText===""){
return true;
}
if (data["im:name"].label.toLowerCase().includes(searchText.toLowerCase())) {
return true;
}
if (data["im:name"].label.includes(searchText)) {
return true;
}
return false;
})
.slice(0, maxResults);

}


In Component AppRecommend:



class AppRecommmend extends PureComponent{
constructor(props){
super(props);
this.state = {
isLoading:false,
};
}
render(){
console.log(this.props.appRecommendData.type); //will show undefined
return(
<div className="component-AppRecommend">
{this.props.appRecommendData.map(appRecommendData=>(
<AppRecommmendCol
imgSource={appRecommendData["im:image"][1].label}
title={appRecommendData["im:name"].label}/>
))}
</div>
);}
}
AppRecommmend.propTypes = {
appRecommendData: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array
])
}
export default AppRecommmend









share|improve this question

























  • Please try and format your code, as it's quite hard to read. filterAppRecommend is asynchronous, so you can't use x directly the way you do in the constructor of App.

    – Tholle
    Nov 15 '18 at 14:55











  • Formatted some of them, see if it is better. Thank you so much!

    – Eric Kwan
    Nov 15 '18 at 15:00
















0












0








0








I am a beginner at ReactJS, I aim to make an app searching web application. Here is my idea:

In app.js, I have a state: appRecommendData which retrieve the result from the app store API and do searching by a function filterAppRecommend(), and then rendered by a component called AppRecommend, I found that I cannot render the result initially, but it works after typing, because of the async problem(when I still fetching the data, the export function have started), then I tried to add async and await to solve it, it works but the props.map is not function, can anyone help to solve it/suggest other way to fix it? below is my code:

app.js:



class App extends Component {
constructor(props) {
super(props);
this.state = {
filteredAppList: ,
filteredRecommend:filterAppRecommend("",10)
};
}
handleSearchChange = event =>{
this.setState({
filteredRecommend:filterAppRecommend(event.target.value,10)
});
}
render() {

return (
<div className="App">
<header className="App-header">
<div className="search-bar"><SearchInput textChange={this.handleSearchChange} /></div>
<div className="App-recommendation">
<AppRecommend appRecommendData={this.state.filteredRecommend}/>
</div>
</header>
</div>
);
}
}
}


InfilterAppRecommend.js:



export default async function filterAppRecommend(searchText, maxResults) {
console.log("maxResults is"+maxResults);
const api = await fetch("https://itunes.apple.com/hk/rss/topgrossingapplications/limit=10/json")
.then(results=>results.json());
console.log(api);
let datas = api.feed.entry;
console.log(datas);
return datas.filter(data => {
if(searchText===""){
return true;
}
if (data["im:name"].label.toLowerCase().includes(searchText.toLowerCase())) {
return true;
}
if (data["im:name"].label.includes(searchText)) {
return true;
}
return false;
})
.slice(0, maxResults);

}


In Component AppRecommend:



class AppRecommmend extends PureComponent{
constructor(props){
super(props);
this.state = {
isLoading:false,
};
}
render(){
console.log(this.props.appRecommendData.type); //will show undefined
return(
<div className="component-AppRecommend">
{this.props.appRecommendData.map(appRecommendData=>(
<AppRecommmendCol
imgSource={appRecommendData["im:image"][1].label}
title={appRecommendData["im:name"].label}/>
))}
</div>
);}
}
AppRecommmend.propTypes = {
appRecommendData: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array
])
}
export default AppRecommmend









share|improve this question
















I am a beginner at ReactJS, I aim to make an app searching web application. Here is my idea:

In app.js, I have a state: appRecommendData which retrieve the result from the app store API and do searching by a function filterAppRecommend(), and then rendered by a component called AppRecommend, I found that I cannot render the result initially, but it works after typing, because of the async problem(when I still fetching the data, the export function have started), then I tried to add async and await to solve it, it works but the props.map is not function, can anyone help to solve it/suggest other way to fix it? below is my code:

app.js:



class App extends Component {
constructor(props) {
super(props);
this.state = {
filteredAppList: ,
filteredRecommend:filterAppRecommend("",10)
};
}
handleSearchChange = event =>{
this.setState({
filteredRecommend:filterAppRecommend(event.target.value,10)
});
}
render() {

return (
<div className="App">
<header className="App-header">
<div className="search-bar"><SearchInput textChange={this.handleSearchChange} /></div>
<div className="App-recommendation">
<AppRecommend appRecommendData={this.state.filteredRecommend}/>
</div>
</header>
</div>
);
}
}
}


InfilterAppRecommend.js:



export default async function filterAppRecommend(searchText, maxResults) {
console.log("maxResults is"+maxResults);
const api = await fetch("https://itunes.apple.com/hk/rss/topgrossingapplications/limit=10/json")
.then(results=>results.json());
console.log(api);
let datas = api.feed.entry;
console.log(datas);
return datas.filter(data => {
if(searchText===""){
return true;
}
if (data["im:name"].label.toLowerCase().includes(searchText.toLowerCase())) {
return true;
}
if (data["im:name"].label.includes(searchText)) {
return true;
}
return false;
})
.slice(0, maxResults);

}


In Component AppRecommend:



class AppRecommmend extends PureComponent{
constructor(props){
super(props);
this.state = {
isLoading:false,
};
}
render(){
console.log(this.props.appRecommendData.type); //will show undefined
return(
<div className="component-AppRecommend">
{this.props.appRecommendData.map(appRecommendData=>(
<AppRecommmendCol
imgSource={appRecommendData["im:image"][1].label}
title={appRecommendData["im:name"].label}/>
))}
</div>
);}
}
AppRecommmend.propTypes = {
appRecommendData: PropTypes.oneOfType([
PropTypes.object,
PropTypes.array
])
}
export default AppRecommmend






javascript ajax reactjs web-applications async-await






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:56







Eric Kwan

















asked Nov 15 '18 at 14:52









Eric KwanEric Kwan

11




11













  • Please try and format your code, as it's quite hard to read. filterAppRecommend is asynchronous, so you can't use x directly the way you do in the constructor of App.

    – Tholle
    Nov 15 '18 at 14:55











  • Formatted some of them, see if it is better. Thank you so much!

    – Eric Kwan
    Nov 15 '18 at 15:00





















  • Please try and format your code, as it's quite hard to read. filterAppRecommend is asynchronous, so you can't use x directly the way you do in the constructor of App.

    – Tholle
    Nov 15 '18 at 14:55











  • Formatted some of them, see if it is better. Thank you so much!

    – Eric Kwan
    Nov 15 '18 at 15:00



















Please try and format your code, as it's quite hard to read. filterAppRecommend is asynchronous, so you can't use x directly the way you do in the constructor of App.

– Tholle
Nov 15 '18 at 14:55





Please try and format your code, as it's quite hard to read. filterAppRecommend is asynchronous, so you can't use x directly the way you do in the constructor of App.

– Tholle
Nov 15 '18 at 14:55













Formatted some of them, see if it is better. Thank you so much!

– Eric Kwan
Nov 15 '18 at 15:00







Formatted some of them, see if it is better. Thank you so much!

– Eric Kwan
Nov 15 '18 at 15:00














1 Answer
1






active

oldest

votes


















0














Since you use .map to go through collection of data, appRecommendData should be an array. filterAppRecommend should return data as array. You can use sth like this to get initial value:



//App.js

async componentDidMount() {
const filteredRecommend = await filterAppRecommend("",10);
this.setState({ filteredRecommend });
}


In AppRecommmend.js use



AppRecommmend.defaultProps = {
appRecommendData :
}





share|improve this answer
























  • It can shows all the results initially but the Type error happens again when I type in the search input.

    – Eric Kwan
    Nov 15 '18 at 15:48











  • So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

    – miuosh
    Nov 15 '18 at 15:51











  • When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

    – miuosh
    Nov 15 '18 at 15:55













  • e is an event in your case

    – miuosh
    Nov 15 '18 at 16:09











  • fixed, thanks to all guys!

    – Eric Kwan
    Nov 15 '18 at 16:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322106%2freact-this-props-apprecommenddata-map-is-not-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Since you use .map to go through collection of data, appRecommendData should be an array. filterAppRecommend should return data as array. You can use sth like this to get initial value:



//App.js

async componentDidMount() {
const filteredRecommend = await filterAppRecommend("",10);
this.setState({ filteredRecommend });
}


In AppRecommmend.js use



AppRecommmend.defaultProps = {
appRecommendData :
}





share|improve this answer
























  • It can shows all the results initially but the Type error happens again when I type in the search input.

    – Eric Kwan
    Nov 15 '18 at 15:48











  • So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

    – miuosh
    Nov 15 '18 at 15:51











  • When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

    – miuosh
    Nov 15 '18 at 15:55













  • e is an event in your case

    – miuosh
    Nov 15 '18 at 16:09











  • fixed, thanks to all guys!

    – Eric Kwan
    Nov 15 '18 at 16:12
















0














Since you use .map to go through collection of data, appRecommendData should be an array. filterAppRecommend should return data as array. You can use sth like this to get initial value:



//App.js

async componentDidMount() {
const filteredRecommend = await filterAppRecommend("",10);
this.setState({ filteredRecommend });
}


In AppRecommmend.js use



AppRecommmend.defaultProps = {
appRecommendData :
}





share|improve this answer
























  • It can shows all the results initially but the Type error happens again when I type in the search input.

    – Eric Kwan
    Nov 15 '18 at 15:48











  • So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

    – miuosh
    Nov 15 '18 at 15:51











  • When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

    – miuosh
    Nov 15 '18 at 15:55













  • e is an event in your case

    – miuosh
    Nov 15 '18 at 16:09











  • fixed, thanks to all guys!

    – Eric Kwan
    Nov 15 '18 at 16:12














0












0








0







Since you use .map to go through collection of data, appRecommendData should be an array. filterAppRecommend should return data as array. You can use sth like this to get initial value:



//App.js

async componentDidMount() {
const filteredRecommend = await filterAppRecommend("",10);
this.setState({ filteredRecommend });
}


In AppRecommmend.js use



AppRecommmend.defaultProps = {
appRecommendData :
}





share|improve this answer













Since you use .map to go through collection of data, appRecommendData should be an array. filterAppRecommend should return data as array. You can use sth like this to get initial value:



//App.js

async componentDidMount() {
const filteredRecommend = await filterAppRecommend("",10);
this.setState({ filteredRecommend });
}


In AppRecommmend.js use



AppRecommmend.defaultProps = {
appRecommendData :
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 15:26









miuoshmiuosh

12312




12312













  • It can shows all the results initially but the Type error happens again when I type in the search input.

    – Eric Kwan
    Nov 15 '18 at 15:48











  • So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

    – miuosh
    Nov 15 '18 at 15:51











  • When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

    – miuosh
    Nov 15 '18 at 15:55













  • e is an event in your case

    – miuosh
    Nov 15 '18 at 16:09











  • fixed, thanks to all guys!

    – Eric Kwan
    Nov 15 '18 at 16:12



















  • It can shows all the results initially but the Type error happens again when I type in the search input.

    – Eric Kwan
    Nov 15 '18 at 15:48











  • So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

    – miuosh
    Nov 15 '18 at 15:51











  • When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

    – miuosh
    Nov 15 '18 at 15:55













  • e is an event in your case

    – miuosh
    Nov 15 '18 at 16:09











  • fixed, thanks to all guys!

    – Eric Kwan
    Nov 15 '18 at 16:12

















It can shows all the results initially but the Type error happens again when I type in the search input.

– Eric Kwan
Nov 15 '18 at 15:48





It can shows all the results initially but the Type error happens again when I type in the search input.

– Eric Kwan
Nov 15 '18 at 15:48













So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

– miuosh
Nov 15 '18 at 15:51





So in handleSearchChange() you can use asyncawait const filteredRecommend = await filterAppRecommend(e.target.value) and then setState(), like in CDM.

– miuosh
Nov 15 '18 at 15:51













When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

– miuosh
Nov 15 '18 at 15:55







When filtering I suggest to use debounce function from lodash library to delay ajax call, because on every key press you will produce new ajax call.

– miuosh
Nov 15 '18 at 15:55















e is an event in your case

– miuosh
Nov 15 '18 at 16:09





e is an event in your case

– miuosh
Nov 15 '18 at 16:09













fixed, thanks to all guys!

– Eric Kwan
Nov 15 '18 at 16:12





fixed, thanks to all guys!

– Eric Kwan
Nov 15 '18 at 16:12




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322106%2freact-this-props-apprecommenddata-map-is-not-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python