Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Displaying Number of Posts and Users | Creating a React-Redux Application
Introduction to Redux
course content

Kursinnhold

Introduction to Redux

Introduction to Redux

1. Getting Started
2. Fundamental Concepts
3. Redux in Practice
4. Creating a React-Redux Application

book
Displaying Number of Posts and Users

We also want to display the number of posts created by a user and the number of posts in general. Apart from that, we also want to show the number of users.

For that, we need to create another selector in the posts slice, which selects posts based on author. Normally selectors cannot take in arguments; however, using the following syntax, we can make a selector which takes in an argument:

js

We also need to keep track of all the users/authors, and for that, we will need to create a new empty array for storing all the authors and updating the list whenever a new author creates a post. By now, the code should look something like this.

Now that we have the relevant selectors, we can import them into index.js and display the data!

In the ProfileDetails, we can use the selector like this:

js

The retrieved data can then be displayed accordingly:

js

Similarly, we can select total posts and authors, and display them in the AppDetails component. The final code for both components should look something like this.

Following is a sandboxed version of the application:

question mark

Which of the following is the correct way to pass an argument into a selector function?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 7

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

Introduction to Redux

Introduction to Redux

1. Getting Started
2. Fundamental Concepts
3. Redux in Practice
4. Creating a React-Redux Application

book
Displaying Number of Posts and Users

We also want to display the number of posts created by a user and the number of posts in general. Apart from that, we also want to show the number of users.

For that, we need to create another selector in the posts slice, which selects posts based on author. Normally selectors cannot take in arguments; however, using the following syntax, we can make a selector which takes in an argument:

js

We also need to keep track of all the users/authors, and for that, we will need to create a new empty array for storing all the authors and updating the list whenever a new author creates a post. By now, the code should look something like this.

Now that we have the relevant selectors, we can import them into index.js and display the data!

In the ProfileDetails, we can use the selector like this:

js

The retrieved data can then be displayed accordingly:

js

Similarly, we can select total posts and authors, and display them in the AppDetails component. The final code for both components should look something like this.

Following is a sandboxed version of the application:

question mark

Which of the following is the correct way to pass an argument into a selector function?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 7
Vi beklager at noe gikk galt. Hva skjedde?
some-alt