React pass data from mapped child to new componentPassing data through React Router to componentsHow get value datapicker in react toobox custom?Accessing props from component when using react-router-dom and reduxReact-redux tutorial : Where does children come from(React) How do you pass in functions to components that are wrapped in a variable?Passing API data from parent container to child component in ReactReact JS - How to access props in a child component which is passed from a parent componentasync data in react-redux child componentA value is undefined in parent-child components connected together with React & Redux. App crashesPassing new state to child component in React

PIC12F675 GP4 doesn't work

How to achieve this rough borders and stippled illustration look?

Cubic programming and beyond?

Why does Hellboy file down his horns?

Print the last, middle and first character of your code

How the name "craqueuhhe" is read

affect vs effect

Replacements for swear words

How to check the quality of an audio sample?

Keep milk (or milk alternative) for a day without a fridge

Why do players in the past play much longer tournaments than today's top players?

What is this welding tool I found in my attic?

What would be the ideal melee weapon made of "Phase Metal"?

How does Fury know about this in Spider-Man: Far From Home?

Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?

How does a Potion of Poison work?

Repeating redundant information after dialogues, to avoid or not?

The monorail explodes before I can get on it

Is purchasing foreign currency before going abroad a losing proposition?

What explains 9 speed cassettes price differences?

Does Ubuntu Server 18.04.2 LTS "track" users in any way?

How can I deal with a player trying to insert real-world mythology into my homebrew setting?

Why did the Japanese attack the Aleutians at the same time as Midway?

Would letting a multiclass character rebuild their character to be single-classed be game-breaking?



React pass data from mapped child to new component


Passing data through React Router to componentsHow get value datapicker in react toobox custom?Accessing props from component when using react-router-dom and reduxReact-redux tutorial : Where does children come from(React) How do you pass in functions to components that are wrapped in a variable?Passing API data from parent container to child component in ReactReact JS - How to access props in a child component which is passed from a parent componentasync data in react-redux child componentA value is undefined in parent-child components connected together with React & Redux. App crashesPassing new state to child component in React






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a shopping catalog, I want to get data from the child and pass to a new component. I've mapped data from JSON (later to be an api call) and I want to get a few props from the mapped child component ie name, price, and the count (which is part of the component, not the original data)



data(name/price) -> mainComponent -> mapped to childComponent(has count)-> mainComponent



should I be using forms? some lifecycle methods? redux? Router? not really sure how to go about this.



Data



const marketData = [

id: 1,
name: "product1",
price: "$2",
,

id: 2,
name: "product2",
price: "$3",
,

id: 2,
name: "product3",
price: "$3",

]


Parent



import React, Component from 'react';
import MarketItem from './MarketItem.js'
import MarketData from './MarketData.js'


class MarketContainer extends Component
constructor()
super()
this.state =
market: MarketData



render()
const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item />)

return (
<div>
marketItems
</div>
);



export default MarketContainer;


child



import React, Component from 'react';

class MarketItem extends Component
constructor(props)
super(props)
this.state =
count : 0




IncrementItem = () =>
this.setState( count: this.state.count + 1 );

IncrementItemBy10 = () =>
this.setState( count: this.state.count + 10 );


render()
return (
<div className="MarketItem">
<div className="market__content">
<h1>this.props.product.name + " " + this.props.product.price</h1>
</div>
<div className="market__counter">
<button className="market__button--minus" onClick=this.DecreaseItem>-</button>
<p>this.state.count</p>
<button className="market__button--plus" onClick=this.IncrementItem>+</button>
</div>

</div>
);


export default MarketItem;


Within parent (marketContainer) I want to get the count, name, and price from every child component, make a new component. I've tried forms, but wasn't really happy with that should I use lifecycle methods? redux? Router?










share|improve this question






















  • Perhaps you should render you <MarketItem> components with a callback prop to notify the <MarketContainer> that the count has changed?

    – Doug Coburn
    Mar 26 at 4:29











  • you could do this by using react refs. but this would not be best way so I'll recommend you to use redux and using redux you can get data on any component.

    – Dhaval
    Mar 26 at 4:29











  • From what I understand, you need the ‘count’ in your MarketContainer?

    – SerShubham
    Mar 26 at 4:29











  • and one more option is you can set state in your parent component and pass callback to child component and when count change call this callback and update state in parent component.

    – Dhaval
    Mar 26 at 4:30

















0















I have a shopping catalog, I want to get data from the child and pass to a new component. I've mapped data from JSON (later to be an api call) and I want to get a few props from the mapped child component ie name, price, and the count (which is part of the component, not the original data)



data(name/price) -> mainComponent -> mapped to childComponent(has count)-> mainComponent



should I be using forms? some lifecycle methods? redux? Router? not really sure how to go about this.



Data



const marketData = [

id: 1,
name: "product1",
price: "$2",
,

id: 2,
name: "product2",
price: "$3",
,

id: 2,
name: "product3",
price: "$3",

]


Parent



import React, Component from 'react';
import MarketItem from './MarketItem.js'
import MarketData from './MarketData.js'


class MarketContainer extends Component
constructor()
super()
this.state =
market: MarketData



render()
const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item />)

return (
<div>
marketItems
</div>
);



export default MarketContainer;


child



import React, Component from 'react';

class MarketItem extends Component
constructor(props)
super(props)
this.state =
count : 0




IncrementItem = () =>
this.setState( count: this.state.count + 1 );

IncrementItemBy10 = () =>
this.setState( count: this.state.count + 10 );


render()
return (
<div className="MarketItem">
<div className="market__content">
<h1>this.props.product.name + " " + this.props.product.price</h1>
</div>
<div className="market__counter">
<button className="market__button--minus" onClick=this.DecreaseItem>-</button>
<p>this.state.count</p>
<button className="market__button--plus" onClick=this.IncrementItem>+</button>
</div>

</div>
);


export default MarketItem;


Within parent (marketContainer) I want to get the count, name, and price from every child component, make a new component. I've tried forms, but wasn't really happy with that should I use lifecycle methods? redux? Router?










share|improve this question






















  • Perhaps you should render you <MarketItem> components with a callback prop to notify the <MarketContainer> that the count has changed?

    – Doug Coburn
    Mar 26 at 4:29











  • you could do this by using react refs. but this would not be best way so I'll recommend you to use redux and using redux you can get data on any component.

    – Dhaval
    Mar 26 at 4:29











  • From what I understand, you need the ‘count’ in your MarketContainer?

    – SerShubham
    Mar 26 at 4:29











  • and one more option is you can set state in your parent component and pass callback to child component and when count change call this callback and update state in parent component.

    – Dhaval
    Mar 26 at 4:30













0












0








0








I have a shopping catalog, I want to get data from the child and pass to a new component. I've mapped data from JSON (later to be an api call) and I want to get a few props from the mapped child component ie name, price, and the count (which is part of the component, not the original data)



data(name/price) -> mainComponent -> mapped to childComponent(has count)-> mainComponent



should I be using forms? some lifecycle methods? redux? Router? not really sure how to go about this.



Data



const marketData = [

id: 1,
name: "product1",
price: "$2",
,

id: 2,
name: "product2",
price: "$3",
,

id: 2,
name: "product3",
price: "$3",

]


Parent



import React, Component from 'react';
import MarketItem from './MarketItem.js'
import MarketData from './MarketData.js'


class MarketContainer extends Component
constructor()
super()
this.state =
market: MarketData



render()
const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item />)

return (
<div>
marketItems
</div>
);



export default MarketContainer;


child



import React, Component from 'react';

class MarketItem extends Component
constructor(props)
super(props)
this.state =
count : 0




IncrementItem = () =>
this.setState( count: this.state.count + 1 );

IncrementItemBy10 = () =>
this.setState( count: this.state.count + 10 );


render()
return (
<div className="MarketItem">
<div className="market__content">
<h1>this.props.product.name + " " + this.props.product.price</h1>
</div>
<div className="market__counter">
<button className="market__button--minus" onClick=this.DecreaseItem>-</button>
<p>this.state.count</p>
<button className="market__button--plus" onClick=this.IncrementItem>+</button>
</div>

</div>
);


export default MarketItem;


Within parent (marketContainer) I want to get the count, name, and price from every child component, make a new component. I've tried forms, but wasn't really happy with that should I use lifecycle methods? redux? Router?










share|improve this question














I have a shopping catalog, I want to get data from the child and pass to a new component. I've mapped data from JSON (later to be an api call) and I want to get a few props from the mapped child component ie name, price, and the count (which is part of the component, not the original data)



data(name/price) -> mainComponent -> mapped to childComponent(has count)-> mainComponent



should I be using forms? some lifecycle methods? redux? Router? not really sure how to go about this.



Data



const marketData = [

id: 1,
name: "product1",
price: "$2",
,

id: 2,
name: "product2",
price: "$3",
,

id: 2,
name: "product3",
price: "$3",

]


Parent



import React, Component from 'react';
import MarketItem from './MarketItem.js'
import MarketData from './MarketData.js'


class MarketContainer extends Component
constructor()
super()
this.state =
market: MarketData



render()
const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item />)

return (
<div>
marketItems
</div>
);



export default MarketContainer;


child



import React, Component from 'react';

class MarketItem extends Component
constructor(props)
super(props)
this.state =
count : 0




IncrementItem = () =>
this.setState( count: this.state.count + 1 );

IncrementItemBy10 = () =>
this.setState( count: this.state.count + 10 );


render()
return (
<div className="MarketItem">
<div className="market__content">
<h1>this.props.product.name + " " + this.props.product.price</h1>
</div>
<div className="market__counter">
<button className="market__button--minus" onClick=this.DecreaseItem>-</button>
<p>this.state.count</p>
<button className="market__button--plus" onClick=this.IncrementItem>+</button>
</div>

</div>
);


export default MarketItem;


Within parent (marketContainer) I want to get the count, name, and price from every child component, make a new component. I've tried forms, but wasn't really happy with that should I use lifecycle methods? redux? Router?







reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 3:58









Chase FenskeChase Fenske

82 bronze badges




82 bronze badges












  • Perhaps you should render you <MarketItem> components with a callback prop to notify the <MarketContainer> that the count has changed?

    – Doug Coburn
    Mar 26 at 4:29











  • you could do this by using react refs. but this would not be best way so I'll recommend you to use redux and using redux you can get data on any component.

    – Dhaval
    Mar 26 at 4:29











  • From what I understand, you need the ‘count’ in your MarketContainer?

    – SerShubham
    Mar 26 at 4:29











  • and one more option is you can set state in your parent component and pass callback to child component and when count change call this callback and update state in parent component.

    – Dhaval
    Mar 26 at 4:30

















  • Perhaps you should render you <MarketItem> components with a callback prop to notify the <MarketContainer> that the count has changed?

    – Doug Coburn
    Mar 26 at 4:29











  • you could do this by using react refs. but this would not be best way so I'll recommend you to use redux and using redux you can get data on any component.

    – Dhaval
    Mar 26 at 4:29











  • From what I understand, you need the ‘count’ in your MarketContainer?

    – SerShubham
    Mar 26 at 4:29











  • and one more option is you can set state in your parent component and pass callback to child component and when count change call this callback and update state in parent component.

    – Dhaval
    Mar 26 at 4:30
















Perhaps you should render you <MarketItem> components with a callback prop to notify the <MarketContainer> that the count has changed?

– Doug Coburn
Mar 26 at 4:29





Perhaps you should render you <MarketItem> components with a callback prop to notify the <MarketContainer> that the count has changed?

– Doug Coburn
Mar 26 at 4:29













you could do this by using react refs. but this would not be best way so I'll recommend you to use redux and using redux you can get data on any component.

– Dhaval
Mar 26 at 4:29





you could do this by using react refs. but this would not be best way so I'll recommend you to use redux and using redux you can get data on any component.

– Dhaval
Mar 26 at 4:29













From what I understand, you need the ‘count’ in your MarketContainer?

– SerShubham
Mar 26 at 4:29





From what I understand, you need the ‘count’ in your MarketContainer?

– SerShubham
Mar 26 at 4:29













and one more option is you can set state in your parent component and pass callback to child component and when count change call this callback and update state in parent component.

– Dhaval
Mar 26 at 4:30





and one more option is you can set state in your parent component and pass callback to child component and when count change call this callback and update state in parent component.

– Dhaval
Mar 26 at 4:30












4 Answers
4






active

oldest

votes


















0














class MarketContainer extends Component 
state =
count : 0


IncrementItem = () =>
this.setState(count: count + 1)


DecreaseItem = () =>
this.setState(count: count - 1)


render()
const marketItems = this.state.market.map(item => <MarketItem key=item.id
product=item IncrementItem=this.IncrementItem DecreaseItem=DecreaseItem count=this.state.count/>)

return (
<div>
marketItems
</div>
);




now you can use IncrementItem and DecreaseItem props on MarketItem component when user click on increment and decrement icon. also we're passing value of count from parent component so you can show in child component.






share|improve this answer






























    0














    You need to use Lifting State Up in React






    share|improve this answer






























      0














      You can define the state in MarketContainer for counts and other fields that you need to pass to other child components.



      I have tried to include the codes to illustrate how I would do that.



       //MarketData.js
      //after ajax request
      const marketData = [


      id: 1,
      name: "product1",
      price: "$1"
      ,


      id: 2,
      name: "product2",
      price: "$2"

      ];

      //We can format market data as per we need it in components

      const formattedMarketData = marketData.map(e =>
      e.count = 0;
      return e;
      );





      //parent component
      //let us define all the things which we might need to pass to other components in here
      // ...

      constructor()

      super();
      this.state =
      markets: formattedMarketData




      /* we are handling all state changes in parent component
      we have updated state in parent component
      we can deliver the changed state to any other components */
      IncrementItem = (id) =>
      this.setState((prevState) =>
      const markets = ...prevState.markets;
      const index = markets.findIndex(x => x.id ===id);
      markets[index].count = prevState.markets[index].count + 1;
      return
      markets: markets
      ;
      );


      DecrementItem = (id) =>
      this.setState((prevState) =>
      const markets = ...prevState.markets;
      const index = markets.findIndex(x => x.id ===id);
      markets[index].count = prevState.markets[index].count - 1;
      return
      markets: markets
      ;
      );


      render()
      const marketItems = this.state.market.map(item => <MarketItem IncrementItem=this.IncrementItem DecrementItem=this.DecrementItem key=item.id product=item />)

      return (
      <div>
      marketItems
      </div>
      );




      //child component

      export default (props) =>
      const IncrementItem, DecreaseItem, product = props;
      <div className="MarketItem">
      <div className="market__content">
      <h1>product.name + " " + product.price</h1>
      </div>
      <div className="market__counter">
      <button className="market__button--minus" onClick=() => DecreaseItem(product.id)>-</button>
      <p>product.count</p>
      <button className="market__button--plus" onClick=() => IncrementItem(product.id)>+</button>
      </div>

      </div>
      ;





      share|improve this answer






























        0














        I think this is the way you should design your state to keep the count relevant to the products.



        import React, Component from 'react';
        import MarketItem from './MarketItem.js'

        class MarketContainer extends Component
        constructor()
        super()
        this.state =
        market: [

        id: 1,
        name: "product1",
        price: "$2",
        count:0,
        ,

        id: 2,
        name: "product2",
        price: "$3",
        count:0,
        ,

        id: 2,
        name: "product3",
        price: "$3",
        count:0,

        ]



        IncrementItem = (i) =>
        let market = this.state.market;
        market[i].count += 1;
        this.setState(market);


        DecreaseItem = (i) =>
        let market = this.state.market;
        market[i].count -= (market[i].count > 0) ? 1: 0;
        this.setState(market);


        render()
        const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item i=i IncrementItem=this.IncrementItem DecreaseItem=this.DecreaseItem/>)

        return (
        <div>
        marketItems
        </div>
        );



        export default MarketContainer;


        child



        import React, Component from 'react';

        class MarketItem extends Component
        constructor(props)
        super(props);


        render()
        return (
        <div className="MarketItem">
        <div className="market__content">
        <h1>this.props.product.name + " " + this.props.product.price</h1>
        </div>
        <div className="market__counter">
        <button className="market__button--minus" onClick=() => this.props.DecreaseItem(this.props.i)>-</button>
        <p>this.state.count</p>

        <button className="market__button--plus" onClick=() => this.props.IncrementItem(this.props.i)>+</button>
        </div>

        </div>
        );



        export default MarketItem;





        share|improve this answer

























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



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349632%2freact-pass-data-from-mapped-child-to-new-component%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          class MarketContainer extends Component 
          state =
          count : 0


          IncrementItem = () =>
          this.setState(count: count + 1)


          DecreaseItem = () =>
          this.setState(count: count - 1)


          render()
          const marketItems = this.state.market.map(item => <MarketItem key=item.id
          product=item IncrementItem=this.IncrementItem DecreaseItem=DecreaseItem count=this.state.count/>)

          return (
          <div>
          marketItems
          </div>
          );




          now you can use IncrementItem and DecreaseItem props on MarketItem component when user click on increment and decrement icon. also we're passing value of count from parent component so you can show in child component.






          share|improve this answer



























            0














            class MarketContainer extends Component 
            state =
            count : 0


            IncrementItem = () =>
            this.setState(count: count + 1)


            DecreaseItem = () =>
            this.setState(count: count - 1)


            render()
            const marketItems = this.state.market.map(item => <MarketItem key=item.id
            product=item IncrementItem=this.IncrementItem DecreaseItem=DecreaseItem count=this.state.count/>)

            return (
            <div>
            marketItems
            </div>
            );




            now you can use IncrementItem and DecreaseItem props on MarketItem component when user click on increment and decrement icon. also we're passing value of count from parent component so you can show in child component.






            share|improve this answer

























              0












              0








              0







              class MarketContainer extends Component 
              state =
              count : 0


              IncrementItem = () =>
              this.setState(count: count + 1)


              DecreaseItem = () =>
              this.setState(count: count - 1)


              render()
              const marketItems = this.state.market.map(item => <MarketItem key=item.id
              product=item IncrementItem=this.IncrementItem DecreaseItem=DecreaseItem count=this.state.count/>)

              return (
              <div>
              marketItems
              </div>
              );




              now you can use IncrementItem and DecreaseItem props on MarketItem component when user click on increment and decrement icon. also we're passing value of count from parent component so you can show in child component.






              share|improve this answer













              class MarketContainer extends Component 
              state =
              count : 0


              IncrementItem = () =>
              this.setState(count: count + 1)


              DecreaseItem = () =>
              this.setState(count: count - 1)


              render()
              const marketItems = this.state.market.map(item => <MarketItem key=item.id
              product=item IncrementItem=this.IncrementItem DecreaseItem=DecreaseItem count=this.state.count/>)

              return (
              <div>
              marketItems
              </div>
              );




              now you can use IncrementItem and DecreaseItem props on MarketItem component when user click on increment and decrement icon. also we're passing value of count from parent component so you can show in child component.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 26 at 4:40









              DhavalDhaval

              5921 gold badge11 silver badges31 bronze badges




              5921 gold badge11 silver badges31 bronze badges























                  0














                  You need to use Lifting State Up in React






                  share|improve this answer



























                    0














                    You need to use Lifting State Up in React






                    share|improve this answer

























                      0












                      0








                      0







                      You need to use Lifting State Up in React






                      share|improve this answer













                      You need to use Lifting State Up in React







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 26 at 4:44









                      umer sayyedumer sayyed

                      562 bronze badges




                      562 bronze badges





















                          0














                          You can define the state in MarketContainer for counts and other fields that you need to pass to other child components.



                          I have tried to include the codes to illustrate how I would do that.



                           //MarketData.js
                          //after ajax request
                          const marketData = [


                          id: 1,
                          name: "product1",
                          price: "$1"
                          ,


                          id: 2,
                          name: "product2",
                          price: "$2"

                          ];

                          //We can format market data as per we need it in components

                          const formattedMarketData = marketData.map(e =>
                          e.count = 0;
                          return e;
                          );





                          //parent component
                          //let us define all the things which we might need to pass to other components in here
                          // ...

                          constructor()

                          super();
                          this.state =
                          markets: formattedMarketData




                          /* we are handling all state changes in parent component
                          we have updated state in parent component
                          we can deliver the changed state to any other components */
                          IncrementItem = (id) =>
                          this.setState((prevState) =>
                          const markets = ...prevState.markets;
                          const index = markets.findIndex(x => x.id ===id);
                          markets[index].count = prevState.markets[index].count + 1;
                          return
                          markets: markets
                          ;
                          );


                          DecrementItem = (id) =>
                          this.setState((prevState) =>
                          const markets = ...prevState.markets;
                          const index = markets.findIndex(x => x.id ===id);
                          markets[index].count = prevState.markets[index].count - 1;
                          return
                          markets: markets
                          ;
                          );


                          render()
                          const marketItems = this.state.market.map(item => <MarketItem IncrementItem=this.IncrementItem DecrementItem=this.DecrementItem key=item.id product=item />)

                          return (
                          <div>
                          marketItems
                          </div>
                          );




                          //child component

                          export default (props) =>
                          const IncrementItem, DecreaseItem, product = props;
                          <div className="MarketItem">
                          <div className="market__content">
                          <h1>product.name + " " + product.price</h1>
                          </div>
                          <div className="market__counter">
                          <button className="market__button--minus" onClick=() => DecreaseItem(product.id)>-</button>
                          <p>product.count</p>
                          <button className="market__button--plus" onClick=() => IncrementItem(product.id)>+</button>
                          </div>

                          </div>
                          ;





                          share|improve this answer



























                            0














                            You can define the state in MarketContainer for counts and other fields that you need to pass to other child components.



                            I have tried to include the codes to illustrate how I would do that.



                             //MarketData.js
                            //after ajax request
                            const marketData = [


                            id: 1,
                            name: "product1",
                            price: "$1"
                            ,


                            id: 2,
                            name: "product2",
                            price: "$2"

                            ];

                            //We can format market data as per we need it in components

                            const formattedMarketData = marketData.map(e =>
                            e.count = 0;
                            return e;
                            );





                            //parent component
                            //let us define all the things which we might need to pass to other components in here
                            // ...

                            constructor()

                            super();
                            this.state =
                            markets: formattedMarketData




                            /* we are handling all state changes in parent component
                            we have updated state in parent component
                            we can deliver the changed state to any other components */
                            IncrementItem = (id) =>
                            this.setState((prevState) =>
                            const markets = ...prevState.markets;
                            const index = markets.findIndex(x => x.id ===id);
                            markets[index].count = prevState.markets[index].count + 1;
                            return
                            markets: markets
                            ;
                            );


                            DecrementItem = (id) =>
                            this.setState((prevState) =>
                            const markets = ...prevState.markets;
                            const index = markets.findIndex(x => x.id ===id);
                            markets[index].count = prevState.markets[index].count - 1;
                            return
                            markets: markets
                            ;
                            );


                            render()
                            const marketItems = this.state.market.map(item => <MarketItem IncrementItem=this.IncrementItem DecrementItem=this.DecrementItem key=item.id product=item />)

                            return (
                            <div>
                            marketItems
                            </div>
                            );




                            //child component

                            export default (props) =>
                            const IncrementItem, DecreaseItem, product = props;
                            <div className="MarketItem">
                            <div className="market__content">
                            <h1>product.name + " " + product.price</h1>
                            </div>
                            <div className="market__counter">
                            <button className="market__button--minus" onClick=() => DecreaseItem(product.id)>-</button>
                            <p>product.count</p>
                            <button className="market__button--plus" onClick=() => IncrementItem(product.id)>+</button>
                            </div>

                            </div>
                            ;





                            share|improve this answer

























                              0












                              0








                              0







                              You can define the state in MarketContainer for counts and other fields that you need to pass to other child components.



                              I have tried to include the codes to illustrate how I would do that.



                               //MarketData.js
                              //after ajax request
                              const marketData = [


                              id: 1,
                              name: "product1",
                              price: "$1"
                              ,


                              id: 2,
                              name: "product2",
                              price: "$2"

                              ];

                              //We can format market data as per we need it in components

                              const formattedMarketData = marketData.map(e =>
                              e.count = 0;
                              return e;
                              );





                              //parent component
                              //let us define all the things which we might need to pass to other components in here
                              // ...

                              constructor()

                              super();
                              this.state =
                              markets: formattedMarketData




                              /* we are handling all state changes in parent component
                              we have updated state in parent component
                              we can deliver the changed state to any other components */
                              IncrementItem = (id) =>
                              this.setState((prevState) =>
                              const markets = ...prevState.markets;
                              const index = markets.findIndex(x => x.id ===id);
                              markets[index].count = prevState.markets[index].count + 1;
                              return
                              markets: markets
                              ;
                              );


                              DecrementItem = (id) =>
                              this.setState((prevState) =>
                              const markets = ...prevState.markets;
                              const index = markets.findIndex(x => x.id ===id);
                              markets[index].count = prevState.markets[index].count - 1;
                              return
                              markets: markets
                              ;
                              );


                              render()
                              const marketItems = this.state.market.map(item => <MarketItem IncrementItem=this.IncrementItem DecrementItem=this.DecrementItem key=item.id product=item />)

                              return (
                              <div>
                              marketItems
                              </div>
                              );




                              //child component

                              export default (props) =>
                              const IncrementItem, DecreaseItem, product = props;
                              <div className="MarketItem">
                              <div className="market__content">
                              <h1>product.name + " " + product.price</h1>
                              </div>
                              <div className="market__counter">
                              <button className="market__button--minus" onClick=() => DecreaseItem(product.id)>-</button>
                              <p>product.count</p>
                              <button className="market__button--plus" onClick=() => IncrementItem(product.id)>+</button>
                              </div>

                              </div>
                              ;





                              share|improve this answer













                              You can define the state in MarketContainer for counts and other fields that you need to pass to other child components.



                              I have tried to include the codes to illustrate how I would do that.



                               //MarketData.js
                              //after ajax request
                              const marketData = [


                              id: 1,
                              name: "product1",
                              price: "$1"
                              ,


                              id: 2,
                              name: "product2",
                              price: "$2"

                              ];

                              //We can format market data as per we need it in components

                              const formattedMarketData = marketData.map(e =>
                              e.count = 0;
                              return e;
                              );





                              //parent component
                              //let us define all the things which we might need to pass to other components in here
                              // ...

                              constructor()

                              super();
                              this.state =
                              markets: formattedMarketData




                              /* we are handling all state changes in parent component
                              we have updated state in parent component
                              we can deliver the changed state to any other components */
                              IncrementItem = (id) =>
                              this.setState((prevState) =>
                              const markets = ...prevState.markets;
                              const index = markets.findIndex(x => x.id ===id);
                              markets[index].count = prevState.markets[index].count + 1;
                              return
                              markets: markets
                              ;
                              );


                              DecrementItem = (id) =>
                              this.setState((prevState) =>
                              const markets = ...prevState.markets;
                              const index = markets.findIndex(x => x.id ===id);
                              markets[index].count = prevState.markets[index].count - 1;
                              return
                              markets: markets
                              ;
                              );


                              render()
                              const marketItems = this.state.market.map(item => <MarketItem IncrementItem=this.IncrementItem DecrementItem=this.DecrementItem key=item.id product=item />)

                              return (
                              <div>
                              marketItems
                              </div>
                              );




                              //child component

                              export default (props) =>
                              const IncrementItem, DecreaseItem, product = props;
                              <div className="MarketItem">
                              <div className="market__content">
                              <h1>product.name + " " + product.price</h1>
                              </div>
                              <div className="market__counter">
                              <button className="market__button--minus" onClick=() => DecreaseItem(product.id)>-</button>
                              <p>product.count</p>
                              <button className="market__button--plus" onClick=() => IncrementItem(product.id)>+</button>
                              </div>

                              </div>
                              ;






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 26 at 5:05









                              Niraj PaudelNiraj Paudel

                              6165 silver badges16 bronze badges




                              6165 silver badges16 bronze badges





















                                  0














                                  I think this is the way you should design your state to keep the count relevant to the products.



                                  import React, Component from 'react';
                                  import MarketItem from './MarketItem.js'

                                  class MarketContainer extends Component
                                  constructor()
                                  super()
                                  this.state =
                                  market: [

                                  id: 1,
                                  name: "product1",
                                  price: "$2",
                                  count:0,
                                  ,

                                  id: 2,
                                  name: "product2",
                                  price: "$3",
                                  count:0,
                                  ,

                                  id: 2,
                                  name: "product3",
                                  price: "$3",
                                  count:0,

                                  ]



                                  IncrementItem = (i) =>
                                  let market = this.state.market;
                                  market[i].count += 1;
                                  this.setState(market);


                                  DecreaseItem = (i) =>
                                  let market = this.state.market;
                                  market[i].count -= (market[i].count > 0) ? 1: 0;
                                  this.setState(market);


                                  render()
                                  const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item i=i IncrementItem=this.IncrementItem DecreaseItem=this.DecreaseItem/>)

                                  return (
                                  <div>
                                  marketItems
                                  </div>
                                  );



                                  export default MarketContainer;


                                  child



                                  import React, Component from 'react';

                                  class MarketItem extends Component
                                  constructor(props)
                                  super(props);


                                  render()
                                  return (
                                  <div className="MarketItem">
                                  <div className="market__content">
                                  <h1>this.props.product.name + " " + this.props.product.price</h1>
                                  </div>
                                  <div className="market__counter">
                                  <button className="market__button--minus" onClick=() => this.props.DecreaseItem(this.props.i)>-</button>
                                  <p>this.state.count</p>

                                  <button className="market__button--plus" onClick=() => this.props.IncrementItem(this.props.i)>+</button>
                                  </div>

                                  </div>
                                  );



                                  export default MarketItem;





                                  share|improve this answer



























                                    0














                                    I think this is the way you should design your state to keep the count relevant to the products.



                                    import React, Component from 'react';
                                    import MarketItem from './MarketItem.js'

                                    class MarketContainer extends Component
                                    constructor()
                                    super()
                                    this.state =
                                    market: [

                                    id: 1,
                                    name: "product1",
                                    price: "$2",
                                    count:0,
                                    ,

                                    id: 2,
                                    name: "product2",
                                    price: "$3",
                                    count:0,
                                    ,

                                    id: 2,
                                    name: "product3",
                                    price: "$3",
                                    count:0,

                                    ]



                                    IncrementItem = (i) =>
                                    let market = this.state.market;
                                    market[i].count += 1;
                                    this.setState(market);


                                    DecreaseItem = (i) =>
                                    let market = this.state.market;
                                    market[i].count -= (market[i].count > 0) ? 1: 0;
                                    this.setState(market);


                                    render()
                                    const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item i=i IncrementItem=this.IncrementItem DecreaseItem=this.DecreaseItem/>)

                                    return (
                                    <div>
                                    marketItems
                                    </div>
                                    );



                                    export default MarketContainer;


                                    child



                                    import React, Component from 'react';

                                    class MarketItem extends Component
                                    constructor(props)
                                    super(props);


                                    render()
                                    return (
                                    <div className="MarketItem">
                                    <div className="market__content">
                                    <h1>this.props.product.name + " " + this.props.product.price</h1>
                                    </div>
                                    <div className="market__counter">
                                    <button className="market__button--minus" onClick=() => this.props.DecreaseItem(this.props.i)>-</button>
                                    <p>this.state.count</p>

                                    <button className="market__button--plus" onClick=() => this.props.IncrementItem(this.props.i)>+</button>
                                    </div>

                                    </div>
                                    );



                                    export default MarketItem;





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      I think this is the way you should design your state to keep the count relevant to the products.



                                      import React, Component from 'react';
                                      import MarketItem from './MarketItem.js'

                                      class MarketContainer extends Component
                                      constructor()
                                      super()
                                      this.state =
                                      market: [

                                      id: 1,
                                      name: "product1",
                                      price: "$2",
                                      count:0,
                                      ,

                                      id: 2,
                                      name: "product2",
                                      price: "$3",
                                      count:0,
                                      ,

                                      id: 2,
                                      name: "product3",
                                      price: "$3",
                                      count:0,

                                      ]



                                      IncrementItem = (i) =>
                                      let market = this.state.market;
                                      market[i].count += 1;
                                      this.setState(market);


                                      DecreaseItem = (i) =>
                                      let market = this.state.market;
                                      market[i].count -= (market[i].count > 0) ? 1: 0;
                                      this.setState(market);


                                      render()
                                      const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item i=i IncrementItem=this.IncrementItem DecreaseItem=this.DecreaseItem/>)

                                      return (
                                      <div>
                                      marketItems
                                      </div>
                                      );



                                      export default MarketContainer;


                                      child



                                      import React, Component from 'react';

                                      class MarketItem extends Component
                                      constructor(props)
                                      super(props);


                                      render()
                                      return (
                                      <div className="MarketItem">
                                      <div className="market__content">
                                      <h1>this.props.product.name + " " + this.props.product.price</h1>
                                      </div>
                                      <div className="market__counter">
                                      <button className="market__button--minus" onClick=() => this.props.DecreaseItem(this.props.i)>-</button>
                                      <p>this.state.count</p>

                                      <button className="market__button--plus" onClick=() => this.props.IncrementItem(this.props.i)>+</button>
                                      </div>

                                      </div>
                                      );



                                      export default MarketItem;





                                      share|improve this answer













                                      I think this is the way you should design your state to keep the count relevant to the products.



                                      import React, Component from 'react';
                                      import MarketItem from './MarketItem.js'

                                      class MarketContainer extends Component
                                      constructor()
                                      super()
                                      this.state =
                                      market: [

                                      id: 1,
                                      name: "product1",
                                      price: "$2",
                                      count:0,
                                      ,

                                      id: 2,
                                      name: "product2",
                                      price: "$3",
                                      count:0,
                                      ,

                                      id: 2,
                                      name: "product3",
                                      price: "$3",
                                      count:0,

                                      ]



                                      IncrementItem = (i) =>
                                      let market = this.state.market;
                                      market[i].count += 1;
                                      this.setState(market);


                                      DecreaseItem = (i) =>
                                      let market = this.state.market;
                                      market[i].count -= (market[i].count > 0) ? 1: 0;
                                      this.setState(market);


                                      render()
                                      const marketItems = this.state.market.map(item => <MarketItem key=item.id product=item i=i IncrementItem=this.IncrementItem DecreaseItem=this.DecreaseItem/>)

                                      return (
                                      <div>
                                      marketItems
                                      </div>
                                      );



                                      export default MarketContainer;


                                      child



                                      import React, Component from 'react';

                                      class MarketItem extends Component
                                      constructor(props)
                                      super(props);


                                      render()
                                      return (
                                      <div className="MarketItem">
                                      <div className="market__content">
                                      <h1>this.props.product.name + " " + this.props.product.price</h1>
                                      </div>
                                      <div className="market__counter">
                                      <button className="market__button--minus" onClick=() => this.props.DecreaseItem(this.props.i)>-</button>
                                      <p>this.state.count</p>

                                      <button className="market__button--plus" onClick=() => this.props.IncrementItem(this.props.i)>+</button>
                                      </div>

                                      </div>
                                      );



                                      export default MarketItem;






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 26 at 5:31









                                      Prabu samvelPrabu samvel

                                      1,0723 silver badges14 bronze badges




                                      1,0723 silver badges14 bronze badges



























                                          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.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349632%2freact-pass-data-from-mapped-child-to-new-component%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

                                          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                          용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                                          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해