Re-render App component after child component API get request?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
A little confused here. I've got an App component that does a get API request to populate the page with property cards. My Properties component renders a sidebar to filter those cards. The links within that sidebar are making the right API calls, and getting the right response – but that response isn't causing the App component to re-render with fewer properties.
I presume I need a ComponentDidUpdate method in App, or to expand the ComponentDidUpdate method in Properties, but I'm not sure which. Can someone point me in the right direction?
App component
class App extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidMount()
Axios.get('http://localhost:3000/api/v1/PropertyListing')
.then((response) =>
this.setState(
properties: response.data,
);
);
render()
return (
<div className="navigation">
<NavBar />
<Switch>
<Route exact path="/" component=Properties />
<Route exact path="/add-property" component=AddProperty />
</Switch>
<PropertyCards
properties=this.state.properties
/>
</div>
);
Properties component
class Properties extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidUpdate(prevProps)
const search = this.props.location;
if (prevProps.location.search !== search)
Axios.get(`http://localhost:3000/api/v1/PropertyListing$search`)
.then(( data: properties ) => this.setState( properties ))
.catch(err => console.error(err));
render()
return (
<div className="sidebar">
<h4>Filter by city</h4>
<div className="filters">
<Link className="filter" to='/?query="city":"Manchester"'>Manchester</Link>
<Link className="filter" to='/?query="city":"Leeds"'>Leeds</Link>
<Link className="filter" to='/?query="city":"Sheffield"'>Sheffield</Link>
<Link className="filter" to='/?query="city":"Liverpool"'>Liverpool</Link>
</div>
</div>
);
reactjs components rendering
add a comment |
A little confused here. I've got an App component that does a get API request to populate the page with property cards. My Properties component renders a sidebar to filter those cards. The links within that sidebar are making the right API calls, and getting the right response – but that response isn't causing the App component to re-render with fewer properties.
I presume I need a ComponentDidUpdate method in App, or to expand the ComponentDidUpdate method in Properties, but I'm not sure which. Can someone point me in the right direction?
App component
class App extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidMount()
Axios.get('http://localhost:3000/api/v1/PropertyListing')
.then((response) =>
this.setState(
properties: response.data,
);
);
render()
return (
<div className="navigation">
<NavBar />
<Switch>
<Route exact path="/" component=Properties />
<Route exact path="/add-property" component=AddProperty />
</Switch>
<PropertyCards
properties=this.state.properties
/>
</div>
);
Properties component
class Properties extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidUpdate(prevProps)
const search = this.props.location;
if (prevProps.location.search !== search)
Axios.get(`http://localhost:3000/api/v1/PropertyListing$search`)
.then(( data: properties ) => this.setState( properties ))
.catch(err => console.error(err));
render()
return (
<div className="sidebar">
<h4>Filter by city</h4>
<div className="filters">
<Link className="filter" to='/?query="city":"Manchester"'>Manchester</Link>
<Link className="filter" to='/?query="city":"Leeds"'>Leeds</Link>
<Link className="filter" to='/?query="city":"Sheffield"'>Sheffield</Link>
<Link className="filter" to='/?query="city":"Liverpool"'>Liverpool</Link>
</div>
</div>
);
reactjs components rendering
add a comment |
A little confused here. I've got an App component that does a get API request to populate the page with property cards. My Properties component renders a sidebar to filter those cards. The links within that sidebar are making the right API calls, and getting the right response – but that response isn't causing the App component to re-render with fewer properties.
I presume I need a ComponentDidUpdate method in App, or to expand the ComponentDidUpdate method in Properties, but I'm not sure which. Can someone point me in the right direction?
App component
class App extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidMount()
Axios.get('http://localhost:3000/api/v1/PropertyListing')
.then((response) =>
this.setState(
properties: response.data,
);
);
render()
return (
<div className="navigation">
<NavBar />
<Switch>
<Route exact path="/" component=Properties />
<Route exact path="/add-property" component=AddProperty />
</Switch>
<PropertyCards
properties=this.state.properties
/>
</div>
);
Properties component
class Properties extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidUpdate(prevProps)
const search = this.props.location;
if (prevProps.location.search !== search)
Axios.get(`http://localhost:3000/api/v1/PropertyListing$search`)
.then(( data: properties ) => this.setState( properties ))
.catch(err => console.error(err));
render()
return (
<div className="sidebar">
<h4>Filter by city</h4>
<div className="filters">
<Link className="filter" to='/?query="city":"Manchester"'>Manchester</Link>
<Link className="filter" to='/?query="city":"Leeds"'>Leeds</Link>
<Link className="filter" to='/?query="city":"Sheffield"'>Sheffield</Link>
<Link className="filter" to='/?query="city":"Liverpool"'>Liverpool</Link>
</div>
</div>
);
reactjs components rendering
A little confused here. I've got an App component that does a get API request to populate the page with property cards. My Properties component renders a sidebar to filter those cards. The links within that sidebar are making the right API calls, and getting the right response – but that response isn't causing the App component to re-render with fewer properties.
I presume I need a ComponentDidUpdate method in App, or to expand the ComponentDidUpdate method in Properties, but I'm not sure which. Can someone point me in the right direction?
App component
class App extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidMount()
Axios.get('http://localhost:3000/api/v1/PropertyListing')
.then((response) =>
this.setState(
properties: response.data,
);
);
render()
return (
<div className="navigation">
<NavBar />
<Switch>
<Route exact path="/" component=Properties />
<Route exact path="/add-property" component=AddProperty />
</Switch>
<PropertyCards
properties=this.state.properties
/>
</div>
);
Properties component
class Properties extends React.Component
constructor(props)
super(props);
this.state =
properties: ,
;
componentDidUpdate(prevProps)
const search = this.props.location;
if (prevProps.location.search !== search)
Axios.get(`http://localhost:3000/api/v1/PropertyListing$search`)
.then(( data: properties ) => this.setState( properties ))
.catch(err => console.error(err));
render()
return (
<div className="sidebar">
<h4>Filter by city</h4>
<div className="filters">
<Link className="filter" to='/?query="city":"Manchester"'>Manchester</Link>
<Link className="filter" to='/?query="city":"Leeds"'>Leeds</Link>
<Link className="filter" to='/?query="city":"Sheffield"'>Sheffield</Link>
<Link className="filter" to='/?query="city":"Liverpool"'>Liverpool</Link>
</div>
</div>
);
reactjs components rendering
reactjs components rendering
asked Nov 15 '18 at 16:04
J.EdgeJ.Edge
245
245
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In general it would be a good idea to move the second api request up intro your parent component, so you 'Properties' component becomes a 'dumb' component which only renders ui according to the data that is passed. That way you don't have to maintain multiple states.
In that context it would definitely make sense to move ComponentDidUpdate() to your 'App' component as well.
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
1
You need to pass a method to your routes which allow them to update the state of your App component<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
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%2f53323401%2fre-render-app-component-after-child-component-api-get-request%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
In general it would be a good idea to move the second api request up intro your parent component, so you 'Properties' component becomes a 'dumb' component which only renders ui according to the data that is passed. That way you don't have to maintain multiple states.
In that context it would definitely make sense to move ComponentDidUpdate() to your 'App' component as well.
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
1
You need to pass a method to your routes which allow them to update the state of your App component<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
add a comment |
In general it would be a good idea to move the second api request up intro your parent component, so you 'Properties' component becomes a 'dumb' component which only renders ui according to the data that is passed. That way you don't have to maintain multiple states.
In that context it would definitely make sense to move ComponentDidUpdate() to your 'App' component as well.
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
1
You need to pass a method to your routes which allow them to update the state of your App component<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
add a comment |
In general it would be a good idea to move the second api request up intro your parent component, so you 'Properties' component becomes a 'dumb' component which only renders ui according to the data that is passed. That way you don't have to maintain multiple states.
In that context it would definitely make sense to move ComponentDidUpdate() to your 'App' component as well.
In general it would be a good idea to move the second api request up intro your parent component, so you 'Properties' component becomes a 'dumb' component which only renders ui according to the data that is passed. That way you don't have to maintain multiple states.
In that context it would definitely make sense to move ComponentDidUpdate() to your 'App' component as well.
answered Nov 15 '18 at 16:26
Fabian HinsenkampFabian Hinsenkamp
1716
1716
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
1
You need to pass a method to your routes which allow them to update the state of your App component<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
add a comment |
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
1
You need to pass a method to your routes which allow them to update the state of your App component<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
Thanks for getting back on this, Fabian – really appreciate it. I take your point, but as soon as I move ComponentDidUpdate from Properties to App – I get an error saying "search is undefined".
– J.Edge
Nov 15 '18 at 16:53
1
1
You need to pass a method to your routes which allow them to update the state of your App component
<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
You need to pass a method to your routes which allow them to update the state of your App component
<Route path='/' component=Properties setLocation=this.setLocation />
– Fabian Hinsenkamp
Nov 15 '18 at 17:00
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
Still getting the exact same error, even passing that method!
– J.Edge
Nov 15 '18 at 17:12
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%2f53323401%2fre-render-app-component-after-child-component-api-get-request%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