Make a Wordle Clone with React #10 - Showing the Current Guess

Net Ninja · Intermediate ·🌐 Frontend Engineering ·4y ago

Key Takeaways

This video demonstrates how to create a Wordle clone using React, focusing on displaying the current guess by passing it as a prop to the row component and splitting it into an array of individual letters. It uses React to map through the array and output a square for each letter, creating an array of empty squares based on the length of the current guess.

Full Transcript

all right then gang so now we're outputting all the past guesses that we've made into the grid and then we have blank grid rows for any future guesses as well the only problem at the minute is that we're not also outputting the current guess as we type it in before we submit it if we head back to the original wordle game you can see that as we type letters those letters are being imported into the next row the current row for the current guess so that's what we need to do as well and remember we already track the current guess when we use the types and we can see that above the grid right here so all we really need to do is pass that current guess into the row component so that it can use that current guess to output it in the next row along so in the grid component we want to take the current guess which we already have as a prop and we want to pass that into the row component but before we do this let's just think about what we're doing for a minute we want to output the current guess on a single row after the previous guesses from the guesses array have been output right so really we only want to pass the current guess as a prop to the row once for the row that kind of represents the current turn that we're on right now because if we pass it into every row that we output then we're just going to output the current guess on every single row in the grid and we don't want to do that so we want to check what turn we're currently on which we can do by the way because we have that turn prop and then we want to match that against a row number in our grid and where the row number is equal to the current turn number then we want to pass through the current guess prop into the row so let me just code this out first and then we'll go over it again to fully understand it so inside the section where we're mapping through the guesses i'm going to do a quick if check at the top and i'll say if the current turn is triple equal to the index of the guesses array then i want to return a different bit of template in that case now i still want to return a row component so we can still output that and i also still want to pass the key prop into the row which is going to be the index again but this time i also want to pass a current guess prop into the row and the value of that is just going to be the current guess that we have so let me just quickly go through an example here say for example the current turn value is two and that means we've had two previous turns or guesses because the term value starts at zero right and as we map through the guesses array for the first two guesses where the index is zero and then one we just return the standard row component but then when the index is two which matches the turn value which would be two then we return this row instead because this row is for the current turn so we can output the current guess inside it as a user types so we return a row component but also pass through the current guess as well so now only this one row will have that current guess prop value inside it so now we can accept the current guess prop in the row component and this is going to be undefined for five of the rows but it's going to have a value for one of the single rows which represents the current turn so what we want to do after we output the previous guesses is do a check for the current guess prop so we could say if current guess and then open up curly braces and then we could return some kind of template inside here and we would only do this if we have a value for current guess if this is truthy all right but before we return anything i first want to take the current guess which is a string and i want to turn it into a simple array of letters that we can iterate so i'm going to say let letters equal to currentguest.split and invoke that and then as an argument i'm going to pass through an empty string into this and this is basically going to split the word and put each individual letter in its own space in our array called letters so now we can map through that array of letters and we can output a square for each letter so i'm going to return some templates inside parentheses and to begin with i want to return a div with a class of row like the other rows but this time also a class of current as well since this is going to be the current row and then we can use that class to style it differently if we need to later on all right and then we want to map through the letters that we have and we want to output a square for each one so let's say in curly braces letters.map and invoke that and inside that pass a function which will return a template for each letter we also get access to each letter as an argument in that function as well as the index so for each letter we want to have a div which will represent the square and that div needs a key prop which is going to be the index and i'm also going to give it a class called filled because this square on the current row has been filled with a letter right and we might want to style those differently later and then also we need to output the letter itself inside the div all right so now we're mapping through our letters of the current guess and we're outputting a square for each one with the letter inside it but there's one small problem which we're going to see as we preview this in a browser all right then so in the grid you can see i've already had one guess bingo and the solution is pools now i'm going to start typing another guess in a second and what you would expect is the letters to start filling in right here is that right because remember we're mapping through those letters as we get the current guess and we're filling in these boxes but watch what happens i'm going to say something like opens so oh and notice straight away all of the other boxes have gone and we just get this one single box if i type p we get two boxes e three and four and s five and finally it looks fine and if i press enter then it gets colored however when we start writing a letter or rather a word we don't actually get the full row we only get the boxes where there are letters available for the boxes so the problem is that the current guess can be any length between zero and five characters long so say for example it's two characters long that means that we're mapping through an array of length two and only outputting two squares one for each letter when in fact we want to output those two squares with letters in plus three other squares that are going to be empty until we type more letters so in essence we need five squares in the row in total at all times and that would be a combination of squares with letters and then the remaining ones that would be empty but how do we know how many empty squares to output because the length of the current guess is always changing as a user types letters well the number of blank squares is always going to be 5 minus the amount of letters we already have so 5 minus the length of this letters array and we can do something like this pretty easily in react in curly braces down here all we have to do is make an array and then inside it we can say array and then in parentheses 5 minus letter dot length okay or letters.length rather and then we also need to use the spread syntax to spread those elements in our array right here and this is basically going to give us an array of undefined values and the length is always going to be correct if we typed one letter in the current guess then this length is going to be four if we typed three letters in the current guess then this length is going to be 2 and so forth so now for this array we can map through it and output some empty squares so the map function is going to return a template and it's also going to take in the value and the index in the function as well now we don't actually need the value because it's just undefined so we can just name that underscore if we want to but then the index can be used as a key in the div that we're going to return right here and each of these divs is now just going to be an empty square in the row and then hopefully fingers crossed this is also going to work so now at the top here we have squares for the letters in the current guess and at the bottom we have empty squares for the remaining spaces in the current guess all right so now in the browser we can see a previous guess this is the solution and if i start to type now now we no longer see just one or two squares but we see the remaining squares as well that are still empty so this is all working and i could submit this we see four are correct but it goes on to the next row now and i can carry on typing and now this is looking much better

Original Description

⭐⭐ Get the full course now (without ads) on the Net Ninja Pro site: https://netninja.dev/p/make-a-wordle-clone-with-react ⭐⭐ Get access to all free & PREMIUM courses on Net Ninja Pro: https://net-ninja-pro.teachable.com/p/net-ninja-pro/ 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/React-Wordle 🐱‍💻 Modern JavaScript Course: On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja On Udemy - https://www.thenetninja.co.uk/udemy/modern-javascript On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 React Full Course: On Net Ninja Pro - https://www.thenetninja.co.uk/udemy/react-and-firebase On Udemy - https://netninja.dev/p/build-websites-with-react-firebase 🐱‍💻 CSS Animations Course: On YouTube- https://www.youtube.com/watch?v=jgw82b5Y2MU&list=PL4cUxeGkcC9iGYgmEd2dm3zAKzyCGDtM5 🐱‍💻 VS Code - https://code.visualstudio.com/ 🐱‍💻 Official Worlde Game - https://www.nytimes.com/games/wordle/index.html
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This video teaches how to display the current guess in a Wordle clone built with React. It covers passing the current guess as a prop to the row component, splitting it into an array of individual letters, and using React to map through the array and output a square for each letter. The video provides a step-by-step guide on how to implement this feature in a React application.

Key Takeaways
  1. Pass the current guess as a prop to the row component
  2. Check the current turn against the index of the guesses array
  3. Split the current guess string into an array of individual letters
  4. Map through the array to output a square for each letter
  5. Create an array of empty squares based on the length of the current guess
  6. Spread the empty squares into the array of squares
  7. Use spread syntax to create an array of undefined values
  8. Map through array to output empty squares
  9. Use index as key in div for empty squares
💡 Using spread syntax and the map function can help create an array of empty squares based on the length of the current guess, making it easier to display the remaining empty squares as the user types in their guess.

Related Reads

📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Learn how to prepare for a Frontend SDE-2 interview at Wayfair, including online assessments, machine coding, and system design.
Medium · Programming
📰
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Learn how HTMX rebuilt a React SPA in a week, replacing 2 years of maintenance work, and discover the benefits of this alternative approach
Medium · Programming
📰
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Learn the 5 levels of front end engineering to improve your skills and avoid getting stuck in a career rut
Medium · Programming
📰
Browser-Based PDF Editing with Vue 3 and pdf-lib
Learn to build a browser-based PDF editor using Vue 3 and pdf-lib, enabling users to edit PDFs directly in the browser
Dev.to · sunshey
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →