React property disappears
I'm having issues with a passed down prop. I'm trying to render an array of objects in a list. However, the prop returns the results, and then immediately turns it to 'undefined'. Open dev tools to see result in console.
Parent component:
import React, { Component } from 'react';
import './App.css';
import { SearchBar } from '../SearchBar/SearchBar.js';
import { SearchResults } from '../SearchResults/SearchResults.js';
import { Playlist } from '../Playlist/Playlist.js';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
searchResults: [
{
id: 2011,
name: 'What Makes A Man',
artist: 'Man As Machine',
album: 'Nothing but a thing'
},
{
id: 2056,
name: 'Pushpin',
artist: 'Man As Machine',
album: 'Patterns'
},
{
id: 2099,
name: 'Zombie',
artist: 'Man As Machine',
album: 'Patterns'
}
],
playlistName: ''
}
}
render() {
return (
<div>
<h1>Ja<span className="highlight">mmm</span>ing</h1>
<div className="App">
<SearchBar />
<div className="App-playlist">
<SearchResults searchResults={this.state.searchResults}/>
<Playlist />
</div>
</div>
</div>
);
}
}
export default App;
the first child component:
import React from 'react';
import './SearchResults.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class SearchResults extends React.Component {
render () {
return (
<div className="SearchResults">
<h2>Results</h2>
<Tracklist tracks={this.props.searchResults}/>
</div>
)
}
}
The destination child component:
import React from 'react';
import './Tracklist.css';
import { Track } from '../Track/Track.js';
export class Tracklist extends React.Component {
constructor(props) {
super(props);
}
renderTrackList() {
let properties = this.props.tracks;
if (properties === undefined){
return <h3>Sorry, we found no results</h3>
} else {
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
}
}
render () {
return (
<div className="TrackList">
{this.renderTrackList()}
</div>
)
}
}
I have attached the other components just for clarity. They are as follows:
playlist:
import React from 'react';
import './Playlist.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class Playlist extends React.Component {
render() {
return (
<div className="Playlist">
<input defaultValue='New Playlist'/>
<Tracklist />
<a className="Playlist-save">SAVE</a>
</div>
)
}
}
searchBar:
import React from 'react';
import './SearchBar.css';
export class SearchBar extends React.Component {
render() {
return (
<div className="SearchBar">
<input placeholder="Enter A Song, Album, or Artist" />
<a>SEARCH</a>
</div>
);
}
}
Track:
import React from 'react';
import './Track.css';
export class Track extends React.Component {
renderAction (isRemoval) {
if (this.props.isRemoval){
return <a className="Track-action" onClick={this.removeTrack}>-</a>
} else {
return <a className="Track-action" onClick={this.addTrack}>+</a>
}
}
render () {
return (
<div className="Track">
<div className="Track-information">
<h3>{this.props.track.name}</h3>
<p>{this.props.track.artist} | {this.props.track.album}</p>
</div>
<a className="Track-action">{this.renderAction}</a>
</div>
)
}
}
Please note that this is still a work in progress. So a lot of the detail and event handlers still need to be programmed.
javascript reactjs
add a comment |
I'm having issues with a passed down prop. I'm trying to render an array of objects in a list. However, the prop returns the results, and then immediately turns it to 'undefined'. Open dev tools to see result in console.
Parent component:
import React, { Component } from 'react';
import './App.css';
import { SearchBar } from '../SearchBar/SearchBar.js';
import { SearchResults } from '../SearchResults/SearchResults.js';
import { Playlist } from '../Playlist/Playlist.js';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
searchResults: [
{
id: 2011,
name: 'What Makes A Man',
artist: 'Man As Machine',
album: 'Nothing but a thing'
},
{
id: 2056,
name: 'Pushpin',
artist: 'Man As Machine',
album: 'Patterns'
},
{
id: 2099,
name: 'Zombie',
artist: 'Man As Machine',
album: 'Patterns'
}
],
playlistName: ''
}
}
render() {
return (
<div>
<h1>Ja<span className="highlight">mmm</span>ing</h1>
<div className="App">
<SearchBar />
<div className="App-playlist">
<SearchResults searchResults={this.state.searchResults}/>
<Playlist />
</div>
</div>
</div>
);
}
}
export default App;
the first child component:
import React from 'react';
import './SearchResults.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class SearchResults extends React.Component {
render () {
return (
<div className="SearchResults">
<h2>Results</h2>
<Tracklist tracks={this.props.searchResults}/>
</div>
)
}
}
The destination child component:
import React from 'react';
import './Tracklist.css';
import { Track } from '../Track/Track.js';
export class Tracklist extends React.Component {
constructor(props) {
super(props);
}
renderTrackList() {
let properties = this.props.tracks;
if (properties === undefined){
return <h3>Sorry, we found no results</h3>
} else {
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
}
}
render () {
return (
<div className="TrackList">
{this.renderTrackList()}
</div>
)
}
}
I have attached the other components just for clarity. They are as follows:
playlist:
import React from 'react';
import './Playlist.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class Playlist extends React.Component {
render() {
return (
<div className="Playlist">
<input defaultValue='New Playlist'/>
<Tracklist />
<a className="Playlist-save">SAVE</a>
</div>
)
}
}
searchBar:
import React from 'react';
import './SearchBar.css';
export class SearchBar extends React.Component {
render() {
return (
<div className="SearchBar">
<input placeholder="Enter A Song, Album, or Artist" />
<a>SEARCH</a>
</div>
);
}
}
Track:
import React from 'react';
import './Track.css';
export class Track extends React.Component {
renderAction (isRemoval) {
if (this.props.isRemoval){
return <a className="Track-action" onClick={this.removeTrack}>-</a>
} else {
return <a className="Track-action" onClick={this.addTrack}>+</a>
}
}
render () {
return (
<div className="Track">
<div className="Track-information">
<h3>{this.props.track.name}</h3>
<p>{this.props.track.artist} | {this.props.track.album}</p>
</div>
<a className="Track-action">{this.renderAction}</a>
</div>
)
}
}
Please note that this is still a work in progress. So a lot of the detail and event handlers still need to be programmed.
javascript reactjs
add a comment |
I'm having issues with a passed down prop. I'm trying to render an array of objects in a list. However, the prop returns the results, and then immediately turns it to 'undefined'. Open dev tools to see result in console.
Parent component:
import React, { Component } from 'react';
import './App.css';
import { SearchBar } from '../SearchBar/SearchBar.js';
import { SearchResults } from '../SearchResults/SearchResults.js';
import { Playlist } from '../Playlist/Playlist.js';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
searchResults: [
{
id: 2011,
name: 'What Makes A Man',
artist: 'Man As Machine',
album: 'Nothing but a thing'
},
{
id: 2056,
name: 'Pushpin',
artist: 'Man As Machine',
album: 'Patterns'
},
{
id: 2099,
name: 'Zombie',
artist: 'Man As Machine',
album: 'Patterns'
}
],
playlistName: ''
}
}
render() {
return (
<div>
<h1>Ja<span className="highlight">mmm</span>ing</h1>
<div className="App">
<SearchBar />
<div className="App-playlist">
<SearchResults searchResults={this.state.searchResults}/>
<Playlist />
</div>
</div>
</div>
);
}
}
export default App;
the first child component:
import React from 'react';
import './SearchResults.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class SearchResults extends React.Component {
render () {
return (
<div className="SearchResults">
<h2>Results</h2>
<Tracklist tracks={this.props.searchResults}/>
</div>
)
}
}
The destination child component:
import React from 'react';
import './Tracklist.css';
import { Track } from '../Track/Track.js';
export class Tracklist extends React.Component {
constructor(props) {
super(props);
}
renderTrackList() {
let properties = this.props.tracks;
if (properties === undefined){
return <h3>Sorry, we found no results</h3>
} else {
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
}
}
render () {
return (
<div className="TrackList">
{this.renderTrackList()}
</div>
)
}
}
I have attached the other components just for clarity. They are as follows:
playlist:
import React from 'react';
import './Playlist.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class Playlist extends React.Component {
render() {
return (
<div className="Playlist">
<input defaultValue='New Playlist'/>
<Tracklist />
<a className="Playlist-save">SAVE</a>
</div>
)
}
}
searchBar:
import React from 'react';
import './SearchBar.css';
export class SearchBar extends React.Component {
render() {
return (
<div className="SearchBar">
<input placeholder="Enter A Song, Album, or Artist" />
<a>SEARCH</a>
</div>
);
}
}
Track:
import React from 'react';
import './Track.css';
export class Track extends React.Component {
renderAction (isRemoval) {
if (this.props.isRemoval){
return <a className="Track-action" onClick={this.removeTrack}>-</a>
} else {
return <a className="Track-action" onClick={this.addTrack}>+</a>
}
}
render () {
return (
<div className="Track">
<div className="Track-information">
<h3>{this.props.track.name}</h3>
<p>{this.props.track.artist} | {this.props.track.album}</p>
</div>
<a className="Track-action">{this.renderAction}</a>
</div>
)
}
}
Please note that this is still a work in progress. So a lot of the detail and event handlers still need to be programmed.
javascript reactjs
I'm having issues with a passed down prop. I'm trying to render an array of objects in a list. However, the prop returns the results, and then immediately turns it to 'undefined'. Open dev tools to see result in console.
Parent component:
import React, { Component } from 'react';
import './App.css';
import { SearchBar } from '../SearchBar/SearchBar.js';
import { SearchResults } from '../SearchResults/SearchResults.js';
import { Playlist } from '../Playlist/Playlist.js';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
searchResults: [
{
id: 2011,
name: 'What Makes A Man',
artist: 'Man As Machine',
album: 'Nothing but a thing'
},
{
id: 2056,
name: 'Pushpin',
artist: 'Man As Machine',
album: 'Patterns'
},
{
id: 2099,
name: 'Zombie',
artist: 'Man As Machine',
album: 'Patterns'
}
],
playlistName: ''
}
}
render() {
return (
<div>
<h1>Ja<span className="highlight">mmm</span>ing</h1>
<div className="App">
<SearchBar />
<div className="App-playlist">
<SearchResults searchResults={this.state.searchResults}/>
<Playlist />
</div>
</div>
</div>
);
}
}
export default App;
the first child component:
import React from 'react';
import './SearchResults.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class SearchResults extends React.Component {
render () {
return (
<div className="SearchResults">
<h2>Results</h2>
<Tracklist tracks={this.props.searchResults}/>
</div>
)
}
}
The destination child component:
import React from 'react';
import './Tracklist.css';
import { Track } from '../Track/Track.js';
export class Tracklist extends React.Component {
constructor(props) {
super(props);
}
renderTrackList() {
let properties = this.props.tracks;
if (properties === undefined){
return <h3>Sorry, we found no results</h3>
} else {
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
}
}
render () {
return (
<div className="TrackList">
{this.renderTrackList()}
</div>
)
}
}
I have attached the other components just for clarity. They are as follows:
playlist:
import React from 'react';
import './Playlist.css';
import { Tracklist } from '../Tracklist/Tracklist.js';
export class Playlist extends React.Component {
render() {
return (
<div className="Playlist">
<input defaultValue='New Playlist'/>
<Tracklist />
<a className="Playlist-save">SAVE</a>
</div>
)
}
}
searchBar:
import React from 'react';
import './SearchBar.css';
export class SearchBar extends React.Component {
render() {
return (
<div className="SearchBar">
<input placeholder="Enter A Song, Album, or Artist" />
<a>SEARCH</a>
</div>
);
}
}
Track:
import React from 'react';
import './Track.css';
export class Track extends React.Component {
renderAction (isRemoval) {
if (this.props.isRemoval){
return <a className="Track-action" onClick={this.removeTrack}>-</a>
} else {
return <a className="Track-action" onClick={this.addTrack}>+</a>
}
}
render () {
return (
<div className="Track">
<div className="Track-information">
<h3>{this.props.track.name}</h3>
<p>{this.props.track.artist} | {this.props.track.album}</p>
</div>
<a className="Track-action">{this.renderAction}</a>
</div>
)
}
}
Please note that this is still a work in progress. So a lot of the detail and event handlers still need to be programmed.
javascript reactjs
javascript reactjs
edited Nov 13 '18 at 2:44
Cœur
17.6k9104145
17.6k9104145
asked Oct 17 '18 at 18:17
TheDarkNordTheDarkNord
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Instead of
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
write
return properties.map( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
OR
.map without return
return properties.map( track => (
console.log(track);
<Track key={track.id} track={track} />;
))
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
1
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52861251%2freact-property-disappears%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
Instead of
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
write
return properties.map( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
OR
.map without return
return properties.map( track => (
console.log(track);
<Track key={track.id} track={track} />;
))
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
1
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
add a comment |
Instead of
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
write
return properties.map( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
OR
.map without return
return properties.map( track => (
console.log(track);
<Track key={track.id} track={track} />;
))
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
1
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
add a comment |
Instead of
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
write
return properties.map( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
OR
.map without return
return properties.map( track => (
console.log(track);
<Track key={track.id} track={track} />;
))
Instead of
properties.forEach( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
write
return properties.map( track => {
console.log(track);
return <Track key={track.id} track={track} />;
})
OR
.map without return
return properties.map( track => (
console.log(track);
<Track key={track.id} track={track} />;
))
edited Oct 17 '18 at 18:39
Hemadri Dasari
7,90711439
7,90711439
answered Oct 17 '18 at 18:20
AmberlampsAmberlamps
18.8k42943
18.8k42943
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
1
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
add a comment |
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
1
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
IT WORKED!!! haha!. Thank you!. I've been working on this all day.
– TheDarkNord
Oct 17 '18 at 18:24
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
@TheDarkNord would you mark the answer as complete? It'd help others
– Sung Kim
Oct 17 '18 at 18:32
1
1
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Sung Kim . Done. had to wait 10 mins. Thanks again community!
– TheDarkNord
Oct 17 '18 at 18:34
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
@Amberlamps hey I edited your answer and added doing .map without return. Please let me know if that make sense
– Hemadri Dasari
Oct 17 '18 at 18:41
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52861251%2freact-property-disappears%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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