Updating state in child component after asynchronous API request - Spotify API in React
up vote
0
down vote
favorite
I'm developing a playlist generator with the Spotify API
. I'm refactoring my code in React and I'm a bit confused about lifting up state with asynchronous calls.
In my App component
, I have a login
function that authenticates a user then updates the state with the user information. I pass this down through a nav component
to a button component that performs the function then updates the state with the user info. I need to use this user info to re render the sign in button as the user's name and photo.
class App extends Component{
//login function
//loginUser(){}
return(
<NavBar loginClicked={this.loginUser} loggedIn={this.state.loggedIn} user={this.state.curUser ? this.state.curUser: undefined}/>
)
}
class NavBar extends Component{
render(){
return (
<nav>
<div className="logo" aria-label="sign in">
<h1><a href="index.html">Moment Music</a></h1>
</div>
<SignIn loginClicked = {this.props.loginClicked} user={this.props.user ? this.props.user: undefined} loggedIn = {this.props.loggedIn} />
</nav>
);
}
}
class SignIn extends Component{
componentWillReceiveProps(nextProps){
console.log(nextProps);
return this.setState({nextProps});
}
render(){
console.log(this.state.user)
return(
<div className='top-right'>
{this.state.loggedIn ?
( //<div className="logged-in" id='logged-in' aria-label="logged in">
<div className="logged-in" id='profile-nav-bar'>
<img className = "profile-img" id='profile-img' src={this.state.user.profileImg}/>
<h2 id='profile-name'>{this.state.user.username}</h2>
</div>)
//</div>)
:
(<div onClick={this.props.loginClicked} className="sign-in" id="sign-in" aria-label="sign in">
<a className="btn btn-primary">Log in with Spotify</a>
</div>)
}
</div>
)
}
}
I can get the state to update with componentWillRecieveProps
but it doesn't get passed to the render method of SignIn
.
Thanks for you help in advance.
javascript reactjs api asynchronous spotify
add a comment |
up vote
0
down vote
favorite
I'm developing a playlist generator with the Spotify API
. I'm refactoring my code in React and I'm a bit confused about lifting up state with asynchronous calls.
In my App component
, I have a login
function that authenticates a user then updates the state with the user information. I pass this down through a nav component
to a button component that performs the function then updates the state with the user info. I need to use this user info to re render the sign in button as the user's name and photo.
class App extends Component{
//login function
//loginUser(){}
return(
<NavBar loginClicked={this.loginUser} loggedIn={this.state.loggedIn} user={this.state.curUser ? this.state.curUser: undefined}/>
)
}
class NavBar extends Component{
render(){
return (
<nav>
<div className="logo" aria-label="sign in">
<h1><a href="index.html">Moment Music</a></h1>
</div>
<SignIn loginClicked = {this.props.loginClicked} user={this.props.user ? this.props.user: undefined} loggedIn = {this.props.loggedIn} />
</nav>
);
}
}
class SignIn extends Component{
componentWillReceiveProps(nextProps){
console.log(nextProps);
return this.setState({nextProps});
}
render(){
console.log(this.state.user)
return(
<div className='top-right'>
{this.state.loggedIn ?
( //<div className="logged-in" id='logged-in' aria-label="logged in">
<div className="logged-in" id='profile-nav-bar'>
<img className = "profile-img" id='profile-img' src={this.state.user.profileImg}/>
<h2 id='profile-name'>{this.state.user.username}</h2>
</div>)
//</div>)
:
(<div onClick={this.props.loginClicked} className="sign-in" id="sign-in" aria-label="sign in">
<a className="btn btn-primary">Log in with Spotify</a>
</div>)
}
</div>
)
}
}
I can get the state to update with componentWillRecieveProps
but it doesn't get passed to the render method of SignIn
.
Thanks for you help in advance.
javascript reactjs api asynchronous spotify
If you didn't already, you will need to bind this to your loginUser method in the constructor method of the App class. Otherwise your method will not have the correct context to access the this.setState method.this.loginUser = this.loginUser.bind(this);
– Kristoffer Svanmark
Nov 22 at 12:18
@KristofferSvanmark Thanks, I added that but it didn't change anything, any idea what's wrong?
– Conor
Nov 22 at 21:07
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm developing a playlist generator with the Spotify API
. I'm refactoring my code in React and I'm a bit confused about lifting up state with asynchronous calls.
In my App component
, I have a login
function that authenticates a user then updates the state with the user information. I pass this down through a nav component
to a button component that performs the function then updates the state with the user info. I need to use this user info to re render the sign in button as the user's name and photo.
class App extends Component{
//login function
//loginUser(){}
return(
<NavBar loginClicked={this.loginUser} loggedIn={this.state.loggedIn} user={this.state.curUser ? this.state.curUser: undefined}/>
)
}
class NavBar extends Component{
render(){
return (
<nav>
<div className="logo" aria-label="sign in">
<h1><a href="index.html">Moment Music</a></h1>
</div>
<SignIn loginClicked = {this.props.loginClicked} user={this.props.user ? this.props.user: undefined} loggedIn = {this.props.loggedIn} />
</nav>
);
}
}
class SignIn extends Component{
componentWillReceiveProps(nextProps){
console.log(nextProps);
return this.setState({nextProps});
}
render(){
console.log(this.state.user)
return(
<div className='top-right'>
{this.state.loggedIn ?
( //<div className="logged-in" id='logged-in' aria-label="logged in">
<div className="logged-in" id='profile-nav-bar'>
<img className = "profile-img" id='profile-img' src={this.state.user.profileImg}/>
<h2 id='profile-name'>{this.state.user.username}</h2>
</div>)
//</div>)
:
(<div onClick={this.props.loginClicked} className="sign-in" id="sign-in" aria-label="sign in">
<a className="btn btn-primary">Log in with Spotify</a>
</div>)
}
</div>
)
}
}
I can get the state to update with componentWillRecieveProps
but it doesn't get passed to the render method of SignIn
.
Thanks for you help in advance.
javascript reactjs api asynchronous spotify
I'm developing a playlist generator with the Spotify API
. I'm refactoring my code in React and I'm a bit confused about lifting up state with asynchronous calls.
In my App component
, I have a login
function that authenticates a user then updates the state with the user information. I pass this down through a nav component
to a button component that performs the function then updates the state with the user info. I need to use this user info to re render the sign in button as the user's name and photo.
class App extends Component{
//login function
//loginUser(){}
return(
<NavBar loginClicked={this.loginUser} loggedIn={this.state.loggedIn} user={this.state.curUser ? this.state.curUser: undefined}/>
)
}
class NavBar extends Component{
render(){
return (
<nav>
<div className="logo" aria-label="sign in">
<h1><a href="index.html">Moment Music</a></h1>
</div>
<SignIn loginClicked = {this.props.loginClicked} user={this.props.user ? this.props.user: undefined} loggedIn = {this.props.loggedIn} />
</nav>
);
}
}
class SignIn extends Component{
componentWillReceiveProps(nextProps){
console.log(nextProps);
return this.setState({nextProps});
}
render(){
console.log(this.state.user)
return(
<div className='top-right'>
{this.state.loggedIn ?
( //<div className="logged-in" id='logged-in' aria-label="logged in">
<div className="logged-in" id='profile-nav-bar'>
<img className = "profile-img" id='profile-img' src={this.state.user.profileImg}/>
<h2 id='profile-name'>{this.state.user.username}</h2>
</div>)
//</div>)
:
(<div onClick={this.props.loginClicked} className="sign-in" id="sign-in" aria-label="sign in">
<a className="btn btn-primary">Log in with Spotify</a>
</div>)
}
</div>
)
}
}
I can get the state to update with componentWillRecieveProps
but it doesn't get passed to the render method of SignIn
.
Thanks for you help in advance.
javascript reactjs api asynchronous spotify
javascript reactjs api asynchronous spotify
edited Nov 22 at 12:27
Habib M. Farooq
303413
303413
asked Nov 22 at 11:44
Conor
355
355
If you didn't already, you will need to bind this to your loginUser method in the constructor method of the App class. Otherwise your method will not have the correct context to access the this.setState method.this.loginUser = this.loginUser.bind(this);
– Kristoffer Svanmark
Nov 22 at 12:18
@KristofferSvanmark Thanks, I added that but it didn't change anything, any idea what's wrong?
– Conor
Nov 22 at 21:07
add a comment |
If you didn't already, you will need to bind this to your loginUser method in the constructor method of the App class. Otherwise your method will not have the correct context to access the this.setState method.this.loginUser = this.loginUser.bind(this);
– Kristoffer Svanmark
Nov 22 at 12:18
@KristofferSvanmark Thanks, I added that but it didn't change anything, any idea what's wrong?
– Conor
Nov 22 at 21:07
If you didn't already, you will need to bind this to your loginUser method in the constructor method of the App class. Otherwise your method will not have the correct context to access the this.setState method.
this.loginUser = this.loginUser.bind(this);
– Kristoffer Svanmark
Nov 22 at 12:18
If you didn't already, you will need to bind this to your loginUser method in the constructor method of the App class. Otherwise your method will not have the correct context to access the this.setState method.
this.loginUser = this.loginUser.bind(this);
– Kristoffer Svanmark
Nov 22 at 12:18
@KristofferSvanmark Thanks, I added that but it didn't change anything, any idea what's wrong?
– Conor
Nov 22 at 21:07
@KristofferSvanmark Thanks, I added that but it didn't change anything, any idea what's wrong?
– Conor
Nov 22 at 21:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You shouldn't set props as a state in child components. Your componentWillReceiveProps in SignIn is unnecessary. If SignIn component props will change, the render method will be executed. React check state's and props's changes to rerender components. In the SingIn use this.props instead this.state
If you use loginClicked method in child component and you referring to this in the function you should call with proper context. So, the best approach is the use of arrow function: loginUser = () => { // your function here }
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
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',
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%2f53430286%2fupdating-state-in-child-component-after-asynchronous-api-request-spotify-api-i%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
up vote
1
down vote
accepted
You shouldn't set props as a state in child components. Your componentWillReceiveProps in SignIn is unnecessary. If SignIn component props will change, the render method will be executed. React check state's and props's changes to rerender components. In the SingIn use this.props instead this.state
If you use loginClicked method in child component and you referring to this in the function you should call with proper context. So, the best approach is the use of arrow function: loginUser = () => { // your function here }
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
add a comment |
up vote
1
down vote
accepted
You shouldn't set props as a state in child components. Your componentWillReceiveProps in SignIn is unnecessary. If SignIn component props will change, the render method will be executed. React check state's and props's changes to rerender components. In the SingIn use this.props instead this.state
If you use loginClicked method in child component and you referring to this in the function you should call with proper context. So, the best approach is the use of arrow function: loginUser = () => { // your function here }
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You shouldn't set props as a state in child components. Your componentWillReceiveProps in SignIn is unnecessary. If SignIn component props will change, the render method will be executed. React check state's and props's changes to rerender components. In the SingIn use this.props instead this.state
If you use loginClicked method in child component and you referring to this in the function you should call with proper context. So, the best approach is the use of arrow function: loginUser = () => { // your function here }
You shouldn't set props as a state in child components. Your componentWillReceiveProps in SignIn is unnecessary. If SignIn component props will change, the render method will be executed. React check state's and props's changes to rerender components. In the SingIn use this.props instead this.state
If you use loginClicked method in child component and you referring to this in the function you should call with proper context. So, the best approach is the use of arrow function: loginUser = () => { // your function here }
answered Nov 22 at 13:04
AgMr
615
615
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
add a comment |
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
My loginUser function is actually an arrow function, I just ommitted it because it's long. I removed the componentWillRecieveProps and used this.props and it still doesn't update SignIn with the new props, any idea what I'm doing wrong?
– Conor
Nov 22 at 21:04
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Do you use setState to update your state in loginUser? Here is your code with fixes that works: codesandbox.io/s/jjjq7kmpm3
– AgMr
Nov 23 at 8:57
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
Ah thank you, the conditional props were messing it up.
– Conor
Nov 25 at 2:37
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53430286%2fupdating-state-in-child-component-after-asynchronous-api-request-spotify-api-i%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
If you didn't already, you will need to bind this to your loginUser method in the constructor method of the App class. Otherwise your method will not have the correct context to access the this.setState method.
this.loginUser = this.loginUser.bind(this);
– Kristoffer Svanmark
Nov 22 at 12:18
@KristofferSvanmark Thanks, I added that but it didn't change anything, any idea what's wrong?
– Conor
Nov 22 at 21:07