Day 7 of 100days of code.

Hello and welcome to my Day 7 challenge. Today is the day God rested, but .. πŸ˜‚πŸ’ͺπŸ₯°

More differences between states and props:

  • State is Internal data defined and managed within a component while props is external data passed from a parent component.

  • State can be updated by the component itself while Props are immutable, meaning they cannot be changed within the child component.

  • Updating state causes component to re-render and receiving new props causes the component to re-render πŸ€”( Usually when the parent's state has been updated).

  • State is components memory🧠 while props play a role similar to the arguments passed to a function.

Flashcard Application:

How is it going to look?.πŸ‘‡πŸ‘‡

So, first, we create our list array πŸ‘‡

Now we map through the list to render it, this time displaying only the question.πŸ‘‡

Now it looks like this with some styles appliedπŸ‘‡πŸ‘‡

Now, let's make this interactive, and for this, we need State

In Three steps

  • Create our state variable at line 48, set to a default value of nullπŸ‘‡

  • Now we use it in our JSX at line 61 to check if question.id is the selectedId, and conditionally render the answer or questionπŸ‘‡

  • For the third step, in the image above at line 49 πŸ‘†πŸ‘†, we create a function to update the state if question.id === selectedId . Since this happens by clicking a div, we add the onClick event listener on line 58πŸ‘†πŸ‘†.

    Take a look at what happens when I click on the first divπŸ‘‡

    Now let's add some conditions to toggle the first div back to the question when clicked on again .For this we update the function by adding the condition at line 50πŸ‘‡πŸ‘‡

Now, add some dynamic CSS styles for the background color to change when the answer is being displayed at line 59πŸ‘‡πŸ‘‡

There we have it; it looks like this now πŸ‘‡πŸ‘‡πŸ‘‡

Thank you for reading and have a great day.

Β