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?










share|improve this question



























    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?










    share|improve this question

























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 4:13

























      asked Nov 10 at 3:08









      Darren

      220626




      220626






















          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 );
          )






          share|improve this answer




















          • filter was exactly right for what I needed. Cheers.
            – Darren
            Nov 10 at 10:09










          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
          );



          );













          draft saved

          draft discarded


















          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

























          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 );
          )






          share|improve this answer




















          • filter was exactly right for what I needed. Cheers.
            – Darren
            Nov 10 at 10:09














          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 );
          )






          share|improve this answer




















          • filter was exactly right for what I needed. Cheers.
            – Darren
            Nov 10 at 10:09












          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 );
          )






          share|improve this answer












          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 );
          )







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 5:13









          AlbertS

          836




          836











          • 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




          filter was exactly right for what I needed. Cheers.
          – Darren
          Nov 10 at 10:09

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Darth Vader #20

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Ondo