Use setState to change the value of a grandchild object
up vote
1
down vote
favorite
Everything I have tried from what I can find doesn't seem to be working. I'm really curious how to access and edit grandchild objects located in the state with react. If anyone could tell me what I'm doing wrong, it would be very helpful.
https://codesandbox.io/s/0mo32q85pp
Take a look at the following code...
App.js
lines: 41-58
getHomeworld = URL => {
fetch(URL)
.then(res => {
return res.json();
})
.then(homeWorldObject => {
console.log(homeWorldObject);
// this.setState({ <- Why isn't this working????
// ...this.state.starwarsChars,
// ...this.state.nextPage,
// ...this.state.prevPage,
// ...this.state.starwarsChars.homeworld = homeWorldObject
// });
})
.catch(err => {
throw new Error(err);
});
};
lines: 86-89
<CharacterList
characters={this.state.starwarsChars}
getHomeworld={this.getHomeworld}
/>
CharacterList.js
lines: 8-12
<Character
key={character.name}
characterDetails={character}
getHomeworld={props.getHomeworld}
/>
Character.js
lines: 18-29
{Object.keys(props.characterDetails).includes("homeworld") ? (
<div className="character-homeworld">
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
</div>
) : (
<div className="character-homeworld">
<h4>Homeworld</h4>
<p>None</p>
</div>
)}
Homeworld.js
lines: 7-10
<div className="homeworld-details">
<p>Name: {props.name}</p>
<p>Rotation Period: {props.rotation_period}</p>
</div>
Expected Output:
If you look on the sandbox webpage, the "Name" and "Rotation Period" (Under "Homeworld") should display the values from here: https://swapi.co/api/planets/1/
Is there anyone who can help me figure this out?
EDIT:
I got really close making these changes (using my local machine, the code on the sandbox is still the original)...
App.js
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
for (let character in temp) {
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
}
// console.log(temp);
this.setState({
starwarsChars: temp
});
Character.js
const Character = props => {
props.getHomeworld(props.characterDetails.homeworld);
console.log(props.characterDetails); // returns object{homeworld: {object}}
console.log(props.characterDetails.homeworld); // returns url
and...
<div className="character-homeworld">
<Homeworld
homeworld={props.characterDetails.homeworld}/>
</div>
However, the issue now is if I do console.log(props.characterDetails.homeworld);
, it logs homeworld: url
and...
if I do console.log(props.characterDetails);
, it logs the property of the character object as homeworld: {object}
...
What I want is the 2nd one, and I'm not sure why it's not consistent.
Update
For some strange reason, codesandbox is console logging both urls, and when I run with yarn start, it logs the url for one, and the object for another. Because of this... I am adding the github link here -> https://github.com/jamespagedev/Sprint-Challenge-React-Wars (so the error can properly be reproduced)
Edit 2:
I changed the sandbox code to the following so we are now only worrying about the code in 1 file -> https://codesandbox.io/s/0mo32q85pp
Here is the issue I am now seeing, and I'm not sure how to solve it...
getHomeworld = URL => {
let home;
fetch(URL)
.then(res => res.json())
.then(homeWorldObject => {
home = homeWorldObject;
console.log(home); // home is an object
});
console.log(home); // why is home undefined?
return home;
};
I've tried doing return homeWorldObject
, but the main function just returns undefined. After doing the console logging, that was what I found, and I'm not sure why that is happening...
css reactjs
add a comment |
up vote
1
down vote
favorite
Everything I have tried from what I can find doesn't seem to be working. I'm really curious how to access and edit grandchild objects located in the state with react. If anyone could tell me what I'm doing wrong, it would be very helpful.
https://codesandbox.io/s/0mo32q85pp
Take a look at the following code...
App.js
lines: 41-58
getHomeworld = URL => {
fetch(URL)
.then(res => {
return res.json();
})
.then(homeWorldObject => {
console.log(homeWorldObject);
// this.setState({ <- Why isn't this working????
// ...this.state.starwarsChars,
// ...this.state.nextPage,
// ...this.state.prevPage,
// ...this.state.starwarsChars.homeworld = homeWorldObject
// });
})
.catch(err => {
throw new Error(err);
});
};
lines: 86-89
<CharacterList
characters={this.state.starwarsChars}
getHomeworld={this.getHomeworld}
/>
CharacterList.js
lines: 8-12
<Character
key={character.name}
characterDetails={character}
getHomeworld={props.getHomeworld}
/>
Character.js
lines: 18-29
{Object.keys(props.characterDetails).includes("homeworld") ? (
<div className="character-homeworld">
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
</div>
) : (
<div className="character-homeworld">
<h4>Homeworld</h4>
<p>None</p>
</div>
)}
Homeworld.js
lines: 7-10
<div className="homeworld-details">
<p>Name: {props.name}</p>
<p>Rotation Period: {props.rotation_period}</p>
</div>
Expected Output:
If you look on the sandbox webpage, the "Name" and "Rotation Period" (Under "Homeworld") should display the values from here: https://swapi.co/api/planets/1/
Is there anyone who can help me figure this out?
EDIT:
I got really close making these changes (using my local machine, the code on the sandbox is still the original)...
App.js
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
for (let character in temp) {
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
}
// console.log(temp);
this.setState({
starwarsChars: temp
});
Character.js
const Character = props => {
props.getHomeworld(props.characterDetails.homeworld);
console.log(props.characterDetails); // returns object{homeworld: {object}}
console.log(props.characterDetails.homeworld); // returns url
and...
<div className="character-homeworld">
<Homeworld
homeworld={props.characterDetails.homeworld}/>
</div>
However, the issue now is if I do console.log(props.characterDetails.homeworld);
, it logs homeworld: url
and...
if I do console.log(props.characterDetails);
, it logs the property of the character object as homeworld: {object}
...
What I want is the 2nd one, and I'm not sure why it's not consistent.
Update
For some strange reason, codesandbox is console logging both urls, and when I run with yarn start, it logs the url for one, and the object for another. Because of this... I am adding the github link here -> https://github.com/jamespagedev/Sprint-Challenge-React-Wars (so the error can properly be reproduced)
Edit 2:
I changed the sandbox code to the following so we are now only worrying about the code in 1 file -> https://codesandbox.io/s/0mo32q85pp
Here is the issue I am now seeing, and I'm not sure how to solve it...
getHomeworld = URL => {
let home;
fetch(URL)
.then(res => res.json())
.then(homeWorldObject => {
home = homeWorldObject;
console.log(home); // home is an object
});
console.log(home); // why is home undefined?
return home;
};
I've tried doing return homeWorldObject
, but the main function just returns undefined. After doing the console logging, that was what I found, and I'm not sure why that is happening...
css reactjs
Try this - this.setState({ startwarsChars: { ...this.state.startwarsChars, homeWorld: homeWorldObject } })
– vs1682
23 hours ago
still not working...
– Fiddle Freak
22 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Everything I have tried from what I can find doesn't seem to be working. I'm really curious how to access and edit grandchild objects located in the state with react. If anyone could tell me what I'm doing wrong, it would be very helpful.
https://codesandbox.io/s/0mo32q85pp
Take a look at the following code...
App.js
lines: 41-58
getHomeworld = URL => {
fetch(URL)
.then(res => {
return res.json();
})
.then(homeWorldObject => {
console.log(homeWorldObject);
// this.setState({ <- Why isn't this working????
// ...this.state.starwarsChars,
// ...this.state.nextPage,
// ...this.state.prevPage,
// ...this.state.starwarsChars.homeworld = homeWorldObject
// });
})
.catch(err => {
throw new Error(err);
});
};
lines: 86-89
<CharacterList
characters={this.state.starwarsChars}
getHomeworld={this.getHomeworld}
/>
CharacterList.js
lines: 8-12
<Character
key={character.name}
characterDetails={character}
getHomeworld={props.getHomeworld}
/>
Character.js
lines: 18-29
{Object.keys(props.characterDetails).includes("homeworld") ? (
<div className="character-homeworld">
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
</div>
) : (
<div className="character-homeworld">
<h4>Homeworld</h4>
<p>None</p>
</div>
)}
Homeworld.js
lines: 7-10
<div className="homeworld-details">
<p>Name: {props.name}</p>
<p>Rotation Period: {props.rotation_period}</p>
</div>
Expected Output:
If you look on the sandbox webpage, the "Name" and "Rotation Period" (Under "Homeworld") should display the values from here: https://swapi.co/api/planets/1/
Is there anyone who can help me figure this out?
EDIT:
I got really close making these changes (using my local machine, the code on the sandbox is still the original)...
App.js
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
for (let character in temp) {
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
}
// console.log(temp);
this.setState({
starwarsChars: temp
});
Character.js
const Character = props => {
props.getHomeworld(props.characterDetails.homeworld);
console.log(props.characterDetails); // returns object{homeworld: {object}}
console.log(props.characterDetails.homeworld); // returns url
and...
<div className="character-homeworld">
<Homeworld
homeworld={props.characterDetails.homeworld}/>
</div>
However, the issue now is if I do console.log(props.characterDetails.homeworld);
, it logs homeworld: url
and...
if I do console.log(props.characterDetails);
, it logs the property of the character object as homeworld: {object}
...
What I want is the 2nd one, and I'm not sure why it's not consistent.
Update
For some strange reason, codesandbox is console logging both urls, and when I run with yarn start, it logs the url for one, and the object for another. Because of this... I am adding the github link here -> https://github.com/jamespagedev/Sprint-Challenge-React-Wars (so the error can properly be reproduced)
Edit 2:
I changed the sandbox code to the following so we are now only worrying about the code in 1 file -> https://codesandbox.io/s/0mo32q85pp
Here is the issue I am now seeing, and I'm not sure how to solve it...
getHomeworld = URL => {
let home;
fetch(URL)
.then(res => res.json())
.then(homeWorldObject => {
home = homeWorldObject;
console.log(home); // home is an object
});
console.log(home); // why is home undefined?
return home;
};
I've tried doing return homeWorldObject
, but the main function just returns undefined. After doing the console logging, that was what I found, and I'm not sure why that is happening...
css reactjs
Everything I have tried from what I can find doesn't seem to be working. I'm really curious how to access and edit grandchild objects located in the state with react. If anyone could tell me what I'm doing wrong, it would be very helpful.
https://codesandbox.io/s/0mo32q85pp
Take a look at the following code...
App.js
lines: 41-58
getHomeworld = URL => {
fetch(URL)
.then(res => {
return res.json();
})
.then(homeWorldObject => {
console.log(homeWorldObject);
// this.setState({ <- Why isn't this working????
// ...this.state.starwarsChars,
// ...this.state.nextPage,
// ...this.state.prevPage,
// ...this.state.starwarsChars.homeworld = homeWorldObject
// });
})
.catch(err => {
throw new Error(err);
});
};
lines: 86-89
<CharacterList
characters={this.state.starwarsChars}
getHomeworld={this.getHomeworld}
/>
CharacterList.js
lines: 8-12
<Character
key={character.name}
characterDetails={character}
getHomeworld={props.getHomeworld}
/>
Character.js
lines: 18-29
{Object.keys(props.characterDetails).includes("homeworld") ? (
<div className="character-homeworld">
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
</div>
) : (
<div className="character-homeworld">
<h4>Homeworld</h4>
<p>None</p>
</div>
)}
Homeworld.js
lines: 7-10
<div className="homeworld-details">
<p>Name: {props.name}</p>
<p>Rotation Period: {props.rotation_period}</p>
</div>
Expected Output:
If you look on the sandbox webpage, the "Name" and "Rotation Period" (Under "Homeworld") should display the values from here: https://swapi.co/api/planets/1/
Is there anyone who can help me figure this out?
EDIT:
I got really close making these changes (using my local machine, the code on the sandbox is still the original)...
App.js
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
for (let character in temp) {
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
}
// console.log(temp);
this.setState({
starwarsChars: temp
});
Character.js
const Character = props => {
props.getHomeworld(props.characterDetails.homeworld);
console.log(props.characterDetails); // returns object{homeworld: {object}}
console.log(props.characterDetails.homeworld); // returns url
and...
<div className="character-homeworld">
<Homeworld
homeworld={props.characterDetails.homeworld}/>
</div>
However, the issue now is if I do console.log(props.characterDetails.homeworld);
, it logs homeworld: url
and...
if I do console.log(props.characterDetails);
, it logs the property of the character object as homeworld: {object}
...
What I want is the 2nd one, and I'm not sure why it's not consistent.
Update
For some strange reason, codesandbox is console logging both urls, and when I run with yarn start, it logs the url for one, and the object for another. Because of this... I am adding the github link here -> https://github.com/jamespagedev/Sprint-Challenge-React-Wars (so the error can properly be reproduced)
Edit 2:
I changed the sandbox code to the following so we are now only worrying about the code in 1 file -> https://codesandbox.io/s/0mo32q85pp
Here is the issue I am now seeing, and I'm not sure how to solve it...
getHomeworld = URL => {
let home;
fetch(URL)
.then(res => res.json())
.then(homeWorldObject => {
home = homeWorldObject;
console.log(home); // home is an object
});
console.log(home); // why is home undefined?
return home;
};
I've tried doing return homeWorldObject
, but the main function just returns undefined. After doing the console logging, that was what I found, and I'm not sure why that is happening...
css reactjs
css reactjs
edited 10 hours ago
asked yesterday
Fiddle Freak
6891432
6891432
Try this - this.setState({ startwarsChars: { ...this.state.startwarsChars, homeWorld: homeWorldObject } })
– vs1682
23 hours ago
still not working...
– Fiddle Freak
22 hours ago
add a comment |
Try this - this.setState({ startwarsChars: { ...this.state.startwarsChars, homeWorld: homeWorldObject } })
– vs1682
23 hours ago
still not working...
– Fiddle Freak
22 hours ago
Try this - this.setState({ startwarsChars: { ...this.state.startwarsChars, homeWorld: homeWorldObject } })
– vs1682
23 hours ago
Try this - this.setState({ startwarsChars: { ...this.state.startwarsChars, homeWorld: homeWorldObject } })
– vs1682
23 hours ago
still not working...
– Fiddle Freak
22 hours ago
still not working...
– Fiddle Freak
22 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Because setState
doesn't handle nested properties. You can create a dummy object (a clone of states' object), mutate it, and apply in setState
:
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
temp.homeworld = homeWorldObject
this.setState({
starwarsChars: temp
});
p.s. don't use this.state
in setState
. Also don't need to pass in states' properties you are not mutating (like ...nextPage
etc.)
Okay, this is starting to make since, but I'm still getting an error sayingprops.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.
– Fiddle Freak
19 hours ago
so I changedlet temp = {...this.state.starwarsChars}
tolet temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...
– Fiddle Freak
19 hours ago
Replacetemp[character].homeworld === URL
withtemp[character] === URL
also removehomeworld
fromtemp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
add a comment |
up vote
0
down vote
The propblem is the ways that passed properties.
'props.getHomeworld' is a function and have no return value here.So "props.getHomeworld(props.characterDetails.homeworld)" dispatches getHomeworld function,but the whole value is null,so the props in Homeworld component is null.
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
"setState" in getHomeworld in App.js is set state for APP.js not the headline.js.And because getHomeWorld is asynchronous and will dispatch many times.Every time it runs, it will override the last property in App.js.I recommand you pass only the URL to headline and define the getHomeworld function in headline.js
<Homeworld
url={props.characterDetails.homeworld}
/>
About the type question.In this part:
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
You have change the type of homeworld.If "if..." is true, change string to object, however,"if..." is false, the type of homeworld will still be object.I recommend you don't change type, maybe set another property for this.BTW,in CharacterList.js,props.characters is an object not an array,so can't use map for that.
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
so, I'm trying to turn the state object propertyhomeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set propertyprops.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.
– Fiddle Freak
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
|
show 7 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Because setState
doesn't handle nested properties. You can create a dummy object (a clone of states' object), mutate it, and apply in setState
:
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
temp.homeworld = homeWorldObject
this.setState({
starwarsChars: temp
});
p.s. don't use this.state
in setState
. Also don't need to pass in states' properties you are not mutating (like ...nextPage
etc.)
Okay, this is starting to make since, but I'm still getting an error sayingprops.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.
– Fiddle Freak
19 hours ago
so I changedlet temp = {...this.state.starwarsChars}
tolet temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...
– Fiddle Freak
19 hours ago
Replacetemp[character].homeworld === URL
withtemp[character] === URL
also removehomeworld
fromtemp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
add a comment |
up vote
0
down vote
Because setState
doesn't handle nested properties. You can create a dummy object (a clone of states' object), mutate it, and apply in setState
:
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
temp.homeworld = homeWorldObject
this.setState({
starwarsChars: temp
});
p.s. don't use this.state
in setState
. Also don't need to pass in states' properties you are not mutating (like ...nextPage
etc.)
Okay, this is starting to make since, but I'm still getting an error sayingprops.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.
– Fiddle Freak
19 hours ago
so I changedlet temp = {...this.state.starwarsChars}
tolet temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...
– Fiddle Freak
19 hours ago
Replacetemp[character].homeworld === URL
withtemp[character] === URL
also removehomeworld
fromtemp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Because setState
doesn't handle nested properties. You can create a dummy object (a clone of states' object), mutate it, and apply in setState
:
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
temp.homeworld = homeWorldObject
this.setState({
starwarsChars: temp
});
p.s. don't use this.state
in setState
. Also don't need to pass in states' properties you are not mutating (like ...nextPage
etc.)
Because setState
doesn't handle nested properties. You can create a dummy object (a clone of states' object), mutate it, and apply in setState
:
let temp = {...this.state.starwarsChars} // use spread operator to clone it, so you don't mutate state on next line;
temp.homeworld = homeWorldObject
this.setState({
starwarsChars: temp
});
p.s. don't use this.state
in setState
. Also don't need to pass in states' properties you are not mutating (like ...nextPage
etc.)
answered 20 hours ago
SkyHigh
36116
36116
Okay, this is starting to make since, but I'm still getting an error sayingprops.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.
– Fiddle Freak
19 hours ago
so I changedlet temp = {...this.state.starwarsChars}
tolet temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...
– Fiddle Freak
19 hours ago
Replacetemp[character].homeworld === URL
withtemp[character] === URL
also removehomeworld
fromtemp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
add a comment |
Okay, this is starting to make since, but I'm still getting an error sayingprops.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.
– Fiddle Freak
19 hours ago
so I changedlet temp = {...this.state.starwarsChars}
tolet temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...
– Fiddle Freak
19 hours ago
Replacetemp[character].homeworld === URL
withtemp[character] === URL
also removehomeworld
fromtemp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
Okay, this is starting to make since, but I'm still getting an error saying
props.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.– Fiddle Freak
19 hours ago
Okay, this is starting to make since, but I'm still getting an error saying
props.characters.map
is not a function. I'm not sure why this is, since characters is considered an array when being passed through the render.– Fiddle Freak
19 hours ago
so I changed
let temp = {...this.state.starwarsChars}
to let temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...– Fiddle Freak
19 hours ago
so I changed
let temp = {...this.state.starwarsChars}
to let temp = [...this.state.starwarsChars]
, and this seems to go from giving me an error, to sending it into some sort of infinite loop so the information never gets printed on the webpage...– Fiddle Freak
19 hours ago
Replace
temp[character].homeworld === URL
with temp[character] === URL
also remove homeworld
from temp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
Replace
temp[character].homeworld === URL
with temp[character] === URL
also remove homeworld
from temp[character].homeworld = homeWorldObject;
– SkyHigh
17 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
I tried this and it still won't work.
– Fiddle Freak
10 hours ago
add a comment |
up vote
0
down vote
The propblem is the ways that passed properties.
'props.getHomeworld' is a function and have no return value here.So "props.getHomeworld(props.characterDetails.homeworld)" dispatches getHomeworld function,but the whole value is null,so the props in Homeworld component is null.
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
"setState" in getHomeworld in App.js is set state for APP.js not the headline.js.And because getHomeWorld is asynchronous and will dispatch many times.Every time it runs, it will override the last property in App.js.I recommand you pass only the URL to headline and define the getHomeworld function in headline.js
<Homeworld
url={props.characterDetails.homeworld}
/>
About the type question.In this part:
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
You have change the type of homeworld.If "if..." is true, change string to object, however,"if..." is false, the type of homeworld will still be object.I recommend you don't change type, maybe set another property for this.BTW,in CharacterList.js,props.characters is an object not an array,so can't use map for that.
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
so, I'm trying to turn the state object propertyhomeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set propertyprops.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.
– Fiddle Freak
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
|
show 7 more comments
up vote
0
down vote
The propblem is the ways that passed properties.
'props.getHomeworld' is a function and have no return value here.So "props.getHomeworld(props.characterDetails.homeworld)" dispatches getHomeworld function,but the whole value is null,so the props in Homeworld component is null.
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
"setState" in getHomeworld in App.js is set state for APP.js not the headline.js.And because getHomeWorld is asynchronous and will dispatch many times.Every time it runs, it will override the last property in App.js.I recommand you pass only the URL to headline and define the getHomeworld function in headline.js
<Homeworld
url={props.characterDetails.homeworld}
/>
About the type question.In this part:
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
You have change the type of homeworld.If "if..." is true, change string to object, however,"if..." is false, the type of homeworld will still be object.I recommend you don't change type, maybe set another property for this.BTW,in CharacterList.js,props.characters is an object not an array,so can't use map for that.
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
so, I'm trying to turn the state object propertyhomeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set propertyprops.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.
– Fiddle Freak
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
|
show 7 more comments
up vote
0
down vote
up vote
0
down vote
The propblem is the ways that passed properties.
'props.getHomeworld' is a function and have no return value here.So "props.getHomeworld(props.characterDetails.homeworld)" dispatches getHomeworld function,but the whole value is null,so the props in Homeworld component is null.
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
"setState" in getHomeworld in App.js is set state for APP.js not the headline.js.And because getHomeWorld is asynchronous and will dispatch many times.Every time it runs, it will override the last property in App.js.I recommand you pass only the URL to headline and define the getHomeworld function in headline.js
<Homeworld
url={props.characterDetails.homeworld}
/>
About the type question.In this part:
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
You have change the type of homeworld.If "if..." is true, change string to object, however,"if..." is false, the type of homeworld will still be object.I recommend you don't change type, maybe set another property for this.BTW,in CharacterList.js,props.characters is an object not an array,so can't use map for that.
The propblem is the ways that passed properties.
'props.getHomeworld' is a function and have no return value here.So "props.getHomeworld(props.characterDetails.homeworld)" dispatches getHomeworld function,but the whole value is null,so the props in Homeworld component is null.
<Homeworld
homeworld={props.getHomeworld(props.characterDetails.homeworld)}
/>
"setState" in getHomeworld in App.js is set state for APP.js not the headline.js.And because getHomeWorld is asynchronous and will dispatch many times.Every time it runs, it will override the last property in App.js.I recommand you pass only the URL to headline and define the getHomeworld function in headline.js
<Homeworld
url={props.characterDetails.homeworld}
/>
About the type question.In this part:
if (temp[character].homeworld === URL) {
temp[character].homeworld = homeWorldObject;
}
You have change the type of homeworld.If "if..." is true, change string to object, however,"if..." is false, the type of homeworld will still be object.I recommend you don't change type, maybe set another property for this.BTW,in CharacterList.js,props.characters is an object not an array,so can't use map for that.
edited 15 hours ago
answered 19 hours ago
Root
80717
80717
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
so, I'm trying to turn the state object propertyhomeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set propertyprops.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.
– Fiddle Freak
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
|
show 7 more comments
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
so, I'm trying to turn the state object propertyhomeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set propertyprops.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.
– Fiddle Freak
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Okay, so two questions... 1) Why is the error message saying it can't map through the characters, because "map isn't a recognized function" (my guess is something to do with it reading it as an object and not an array). 2) How would you recommend this function gets property invoked, so data is passed and displayed correctly?
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
Another question I have is I thought every time this.setState gets called, the render() automatically gets called (not needing a return value like in our getCharacters() method)
– Fiddle Freak
19 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
About the first two questions,I have changed my answers.And the third question is yes,every time this.setState changed,the render() will automatically gets called.I don't see return in getCharacters ,you mean "return res.json()"?@FiddleFreak
– Root
18 hours ago
so, I'm trying to turn the state object property
homeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set property props.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.– Fiddle Freak
18 hours ago
so, I'm trying to turn the state object property
homeland
from a url string into the object being passed in the getHomeworld() method. Once this state is set(appended)... I am trying to call the newly set property props.characterDetails.homeworld
and pass it through. I've tried invoking the method on line 5 in Character.js, but it still gets read as undefined.– Fiddle Freak
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
@FiddleFreak This problem is similar to props.characterDetails.homeworld has no return value. The return res.json() is different, you can't get it with line 5 in Character.js.You can either console.log props.getHomeworld or props.characterDetails.homeworld or set a state in app.js for res.json() and pass it to Character.js
– Root
18 hours ago
|
show 7 more comments
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235240%2fuse-setstate-to-change-the-value-of-a-grandchild-object%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
Try this - this.setState({ startwarsChars: { ...this.state.startwarsChars, homeWorld: homeWorldObject } })
– vs1682
23 hours ago
still not working...
– Fiddle Freak
22 hours ago