r/LearnReact • • May 27 '20

I want to check if an object is found in state. Then, if it is, perform a function.

0 Upvotes

I'm building a game where a player purchases and sells items. On purchase I need to add the purchased item to the "player's bag." I want to check if the item already exists in the "player's bag" and if so I want to update that item. If it isn't there already I want to add it.

I've got the function to add it if it doesn't exist already down but I'm having problems checking to see if it exists and then updating it if it does. I've tried using .find and .filter to loop through the state but they both throw errors that ".find/.filter is not a function."

Here's the function currently:

   addItem = (item, quantity, totalPrice) => {
        console.log(item);
     this.setState({
        funds: this.state.funds - totalPrice,
    });
            let items = this.state.playerItems;
    let itemIsInBag = items.filter(item => item.name.indexOf(item.name) >= 0); 

     //* I also used "let itemIsInBag = items.find(matchingItem => matchingItem.name == item.name);" both returned the not a function error

           if (itemIsInBag) {
        const newQuantity = this.matchingItem.quantity + quantity;
        const newPrice = (this.matchingItem.price + totalPrice) / newQuantity;

        const newItem = {
            name: item.name,
            price: newPrice,
            quantity: newQuantity,
                        }
       
    } else {
        const newItem = {
            name: item.name,
            price: totalPrice,
            quantity: quantity,
                        }
        const playerItems = this.state.playerItems.concat(newItem);
        return {
            playerItems
        };
    };
}

Any ideas about what I'm doing wrong or that would be a better way would be greatly appreciated.


r/LearnReact • • Aug 22 '19

The Right Way to Style Material-UI

Thumbnail
blog.bitsrc.io
0 Upvotes

r/LearnReact • • Aug 21 '19

Functional Programming in JavaScript: How and Why

Thumbnail
blog.bitsrc.io
0 Upvotes

r/LearnReact • • Jul 05 '19

Learn to code by cloning Airbnb

Thumbnail
arthuryeti.com
1 Upvotes

r/LearnReact • • Jun 10 '20

5 Alternatives to React Redux in 2020

Thumbnail
blog.bitsrc.io
2 Upvotes

r/LearnReact • • Jul 09 '19

11 React Testing Tools and Libraries for 2019

Thumbnail
blog.bitsrc.io
2 Upvotes

r/LearnReact • • Jun 14 '20

React Router v6 Preview: Nested Routing

Thumbnail
youtube.com
4 Upvotes

r/LearnReact • • Jun 24 '20

Composing Documents with MDX and React: Markdown for the Component Era

Thumbnail
blog.bitsrc.io
1 Upvotes

r/LearnReact • • May 18 '20

Sharing React Components between Your SPA and Your Static Pages

Thumbnail
blog.bitsrc.io
3 Upvotes

r/LearnReact • • Jun 15 '20

React Hook Form VS Formik

Thumbnail
blog.bitsrc.io
3 Upvotes

r/LearnReact • • May 25 '20

Building with React for All Platforms: Top Frameworks and Tools

Thumbnail
blog.bitsrc.io
2 Upvotes

r/LearnReact • • Jun 09 '20

Bring Full-Stack to the JAMstack with RedwoodJS

Thumbnail
blog.bitsrc.io
2 Upvotes

r/LearnReact • • May 26 '20

Transform an SVG into a React Component with SVGR

Thumbnail
blog.bitsrc.io
1 Upvotes

r/LearnReact • • Jun 08 '20

How to Publish React Components

Thumbnail
blog.bitsrc.io
1 Upvotes

r/LearnReact • • May 28 '20

4 Ways to Override Material UI Styles

Thumbnail
blog.bitsrc.io
1 Upvotes

r/LearnReact • • Jun 01 '20

Publish and Reuse React Components to Build Gatsby Sites, Faster

Thumbnail
blog.bitsrc.io
1 Upvotes

r/LearnReact • • Jun 16 '20

Why Frontend Developers Choose Gatsby Over Traditional CMS

Thumbnail
blog.bitsrc.io
2 Upvotes

r/LearnReact • • May 20 '20

How to Sync Your React App with the System Color Scheme

Thumbnail
blog.bitsrc.io
1 Upvotes

r/LearnReact • • Oct 30 '18

Where should you start when learning React?

Thumbnail
revs.runtime-revolution.com
5 Upvotes

r/LearnReact • • Jan 20 '20

React Tutorial

Thumbnail
react-tutorial.app
1 Upvotes

r/LearnReact • • Feb 13 '19

How to get the id of the item that was clicked?

1 Upvotes

Hi All,

I am doing a trivia to learn react and I have this problem. How to know which particular item of the array was clicked?

I thought that adding index would help but seems like that's not enough (console.log shows all the answers for the question)?

Here's my code for now:

<div> <h2>{this.props.trivia.question}</h2> </div> <div> **{this.props.trivia.answers.map((triviaAns, index)=>** <li **key={triviaAns.index}** onClick={()=>this.props.onClick(id)} \> {quizAnswer.ans} **{console.log(\[triviaAns.index\])}**

</li>

)}

</div>


r/LearnReact • • Jul 10 '19

Exporting Data to Excel with React

Thumbnail
blog.bitsrc.io
2 Upvotes

r/LearnReact • • Nov 08 '21

Happy Cakeday, r/LearnReact! Today you're 6

3 Upvotes

Let's look back at some memorable moments and interesting insights from last year.

Your top 1 posts:


r/LearnReact • • Nov 08 '22

Happy Cakeday, r/LearnReact! Today you're 7

1 Upvotes

Let's look back at some memorable moments and interesting insights from last year.

Your top 1 posts:


r/LearnReact • • Jan 23 '19

List of React Tutorials

Thumbnail
cybrhome.com
2 Upvotes