setState from .map inside componentDidMount()
up vote
1
down vote
favorite
I am trying to find a solution to using setState
on mapped items inside componentDidMount
.
I am using GraphQL along with Gatsby with many data
items returned but require that on specific pathname
is ===
to slug
the state
is updated in the component to the matching littleHotelierId
.
propertyInit = () =>
const pathname = location.pathname;
return (
<StaticQuery
query=graphql`
query
allContentfulProperties
edges
node
id
slug
information
littleHotelierId
`
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return null;
);
/>
);
;
Then I am pulling this into componentDidMount
as
componentDidMount()
this.propertyInit();
not relevant but as reference this.isCancelled = true;
is added to componentWillUnmount
.
I don't receive any errors but if I console.log(littleHotelierId)
I get nothing.
I did at first think that it may be because return
is null so tried giving the map
a const
and returning as
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
const littleHotelier =
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return littleHotelier;
);
but this was unsuccessful too.
The Goal is for componentDidMount
to map items returned in the GraphQL data
as
componentDidMount()
if (path1 === '/path-slug1')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-1'
);
if (path2 === '/path-slug2')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-2'
);
... // other items
I think the issue is that GraphQL is fetching data as asynchronous and this request not completed as componentDidMount() is called. If I console.log
the data
it is not returning anything to the console
. How can I fix this?
javascript reactjs graphql gatsby
add a comment |
up vote
1
down vote
favorite
I am trying to find a solution to using setState
on mapped items inside componentDidMount
.
I am using GraphQL along with Gatsby with many data
items returned but require that on specific pathname
is ===
to slug
the state
is updated in the component to the matching littleHotelierId
.
propertyInit = () =>
const pathname = location.pathname;
return (
<StaticQuery
query=graphql`
query
allContentfulProperties
edges
node
id
slug
information
littleHotelierId
`
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return null;
);
/>
);
;
Then I am pulling this into componentDidMount
as
componentDidMount()
this.propertyInit();
not relevant but as reference this.isCancelled = true;
is added to componentWillUnmount
.
I don't receive any errors but if I console.log(littleHotelierId)
I get nothing.
I did at first think that it may be because return
is null so tried giving the map
a const
and returning as
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
const littleHotelier =
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return littleHotelier;
);
but this was unsuccessful too.
The Goal is for componentDidMount
to map items returned in the GraphQL data
as
componentDidMount()
if (path1 === '/path-slug1')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-1'
);
if (path2 === '/path-slug2')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-2'
);
... // other items
I think the issue is that GraphQL is fetching data as asynchronous and this request not completed as componentDidMount() is called. If I console.log
the data
it is not returning anything to the console
. How can I fix this?
javascript reactjs graphql gatsby
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to find a solution to using setState
on mapped items inside componentDidMount
.
I am using GraphQL along with Gatsby with many data
items returned but require that on specific pathname
is ===
to slug
the state
is updated in the component to the matching littleHotelierId
.
propertyInit = () =>
const pathname = location.pathname;
return (
<StaticQuery
query=graphql`
query
allContentfulProperties
edges
node
id
slug
information
littleHotelierId
`
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return null;
);
/>
);
;
Then I am pulling this into componentDidMount
as
componentDidMount()
this.propertyInit();
not relevant but as reference this.isCancelled = true;
is added to componentWillUnmount
.
I don't receive any errors but if I console.log(littleHotelierId)
I get nothing.
I did at first think that it may be because return
is null so tried giving the map
a const
and returning as
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
const littleHotelier =
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return littleHotelier;
);
but this was unsuccessful too.
The Goal is for componentDidMount
to map items returned in the GraphQL data
as
componentDidMount()
if (path1 === '/path-slug1')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-1'
);
if (path2 === '/path-slug2')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-2'
);
... // other items
I think the issue is that GraphQL is fetching data as asynchronous and this request not completed as componentDidMount() is called. If I console.log
the data
it is not returning anything to the console
. How can I fix this?
javascript reactjs graphql gatsby
I am trying to find a solution to using setState
on mapped items inside componentDidMount
.
I am using GraphQL along with Gatsby with many data
items returned but require that on specific pathname
is ===
to slug
the state
is updated in the component to the matching littleHotelierId
.
propertyInit = () =>
const pathname = location.pathname;
return (
<StaticQuery
query=graphql`
query
allContentfulProperties
edges
node
id
slug
information
littleHotelierId
`
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return null;
);
/>
);
;
Then I am pulling this into componentDidMount
as
componentDidMount()
this.propertyInit();
not relevant but as reference this.isCancelled = true;
is added to componentWillUnmount
.
I don't receive any errors but if I console.log(littleHotelierId)
I get nothing.
I did at first think that it may be because return
is null so tried giving the map
a const
and returning as
render=data =>
data.allContentfulProperties.edges.map(( node: property ) =>
if (pathname === property.slug)
const littleHotelier =
!this.isCancelled &&
this.setState(
littleHotelierId: property.information.littleHotelierId
);
return littleHotelier;
);
but this was unsuccessful too.
The Goal is for componentDidMount
to map items returned in the GraphQL data
as
componentDidMount()
if (path1 === '/path-slug1')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-1'
);
if (path2 === '/path-slug2')
!this.isCancelled &&
this.setState(
littleHotelierId: 'path-id-2'
);
... // other items
I think the issue is that GraphQL is fetching data as asynchronous and this request not completed as componentDidMount() is called. If I console.log
the data
it is not returning anything to the console
. How can I fix this?
javascript reactjs graphql gatsby
javascript reactjs graphql gatsby
edited Nov 10 at 4:13
asked Nov 10 at 3:08
Darren
220626
220626
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I think you need to create some filtered data as a result of a map function. After you have filtered data you do setState(data: data). It is not good to do multiple setState.
If your GraphQL returns promise then you can write something like the following:
componentDidMount()
this.fetchData()
.then(data =>
const filteredData = data.filter(element =>
element.someProperty === propertyValue
);
this.setState( data: filteredData );
)
filter
was exactly right for what I needed. Cheers.
– Darren
Nov 10 at 10:09
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I think you need to create some filtered data as a result of a map function. After you have filtered data you do setState(data: data). It is not good to do multiple setState.
If your GraphQL returns promise then you can write something like the following:
componentDidMount()
this.fetchData()
.then(data =>
const filteredData = data.filter(element =>
element.someProperty === propertyValue
);
this.setState( data: filteredData );
)
filter
was exactly right for what I needed. Cheers.
– Darren
Nov 10 at 10:09
add a comment |
up vote
1
down vote
accepted
I think you need to create some filtered data as a result of a map function. After you have filtered data you do setState(data: data). It is not good to do multiple setState.
If your GraphQL returns promise then you can write something like the following:
componentDidMount()
this.fetchData()
.then(data =>
const filteredData = data.filter(element =>
element.someProperty === propertyValue
);
this.setState( data: filteredData );
)
filter
was exactly right for what I needed. Cheers.
– Darren
Nov 10 at 10:09
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I think you need to create some filtered data as a result of a map function. After you have filtered data you do setState(data: data). It is not good to do multiple setState.
If your GraphQL returns promise then you can write something like the following:
componentDidMount()
this.fetchData()
.then(data =>
const filteredData = data.filter(element =>
element.someProperty === propertyValue
);
this.setState( data: filteredData );
)
I think you need to create some filtered data as a result of a map function. After you have filtered data you do setState(data: data). It is not good to do multiple setState.
If your GraphQL returns promise then you can write something like the following:
componentDidMount()
this.fetchData()
.then(data =>
const filteredData = data.filter(element =>
element.someProperty === propertyValue
);
this.setState( data: filteredData );
)
answered Nov 10 at 5:13
AlbertS
836
836
filter
was exactly right for what I needed. Cheers.
– Darren
Nov 10 at 10:09
add a comment |
filter
was exactly right for what I needed. Cheers.
– Darren
Nov 10 at 10:09
filter
was exactly right for what I needed. Cheers.– Darren
Nov 10 at 10:09
filter
was exactly right for what I needed. Cheers.– Darren
Nov 10 at 10:09
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%2f53235689%2fsetstate-from-map-inside-componentdidmount%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