Is it possible for me to create an If statement that can verify the current index within a Map?

I have the following TypeScript code snippet:

export default class SingleNews extends React.Component<INews, {}> {
    public render(): React.ReactElement<INews> {
        return (
            <>
                {this.props.featured
                    ? this.props.recentNews.map(post => (
                
                            <a
                                className={styles.singleNews}
                                href={post.link}
                                key={post.Title}
                            >
                                <div
                                    className={styles.singleNews__image}
                                    style={{ backgroundImage: `url(${post.image})` }}
                                />
                                <div className={styles.singleNews__content}>
                                <div className={styles.singleNews__content__info}>
                                <span className={styles.singleNews__content__info__label}>{post.Featured}</span>
                                <span className={styles.singleNews__content__info__date}>
                                            {post.date}
                                        </span></div>
                                </div>
                                <div className={styles.singleNews__content}>
                                    <div className={styles.singleNews__content__info}>
                                    
                                        <h2 className={styles.singleNews__content__info__title}>
                                            {post.Title}
                                        </h2>
                                    
                                        

                                        {post.likes ? (
                                            <div
                                                className={styles.singleNews__content__info__reactions}
                                            >
                                                <span
                                                    className={
                                                        styles.singleNews__content__info__reactions__likes
                                                    }
                                                >
                                                    <Icon iconName='Like' />
                                                    {post.likes}
                                                </span>
                                                <span>
                                                    <Icon iconName='ActionCenter' />
                                                    {post.coments}
                                                </span>
                                            </div>
                                        ) : null}
                                    </div>
                                </div>
                            </a>
                                                
                      ))

However, I want to dynamically render different HTML based on the current index. When I attempted to use an if statement inside the map function, I encountered an error stating "expression expected" on the if. Can anyone provide suggestions or advice?

Answer №1

The issue arises from using a statement as the body of your arrow function in this scenario.

Now that you need to include an if statement to check the index, you should utilize a block like so:

{this.props.featured
   ? this.props.recentNews.map((post,index) => {  
                if(index=0) { 
                  // add your logic here
                }
                return <a> .... </a>
     )}
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Keeping state between navigations in Ionic and AngularJS: A guide

In the process of developing my very first hybrid mobile application using Ionic and AngularJS, I have encountered a challenge that I am currently trying to resolve. The issue at hand involves maintaining the state of the graphical user interface between n ...

Navigating through various product categories in Angular's routing system

Greetings! I am currently building a Shop Page in Angular 4 and encountering an obstacle with Angular routing. The issue arises when a user clicks on a product category, the intention is for the website to direct them to the shop page. On the homepage, th ...

Connect individuals based on specific criteria within a nested array

My MongoDB collection looks something like this: _id: ObjectId("5cb089e459552d8b8cc6a9e4") username: "admin" password: "12345" gender: "male" interestedIn: "female" movie: Array 0: Object id: "Avatar" title: "Avatar" poster: "~" 1: Object ...

Select multiple rows by checking the checkboxes and select a single row by clicking on it in the MUI DataGrid

I am currently utilizing the MUI DataGrid version 4 component. The desired functionalities are as follows: Allow multiple selections from the checkbox in the Data Grid (if the user selects multiple rows using the checkbox). Prevent multiple selections fr ...

AssistanceBubble.js with ASP.NET UpdatePanel

Looking for guidance on implementing HelpBalloon.js () within an ASP.NET UpdatePanel. Experiencing issues with image loss after a postback. ...

Node.js route leads to a 404 error page due to a simple configuration

Trying to set up two separate routes using NodeJS with the express framework and Angular on the client side. The index page successfully renders at localhost:3000/. However, when attempting to render the login page by visiting localhost:3000/login, a GET / ...

Error Encountered: Nested textarea not supported in HTML

Below is the code I am working with. The issue lies with the <textarea>. In my form, there is a textarea. When I insert another <textarea> within the ckeditor value (HTML), the inner textarea ends up closing the parent textarea. Is there a sol ...

What is the best way to remove any objects in this array that have empty strings as values?

I have been developing a form using Angular 14. The form consists of a primary section, which includes the user's necessary information, and a secondary section where users can input additional data (an array of residences displayed in a table) befor ...

Despite the unconsumedBufferLength being 0, DataReader.loadAsync is still being completed

Working on UWP WinRT, I'm dealing with JSON stream consumption using the following code: async function connect() { let stream: MSStream; return new CancellableContext<void>( async (context) => { stream ...

Utilizing Vue to Embed Multiple Hubspot Forms on one Page

While working on a Vue page, I encountered an issue with loading multiple Hubspot forms simultaneously. Only one form would load at a time. Here is the code snippet I used to append a single Hubspot form: mounted() { const script = document.createElem ...

Angular: Unable to locate route declaration in the specified path /src/app/app-routing.module.ts

Whenever I attempt to set up automatic routing for components that have been created using the command below ng generate module orders --route orders --module app.module I encounter the following error message The requested URL /src/app/app-routing.mod ...

Ways to utilize array reduce for organizing information

Can someone please guide me on how to use the reduce array method to sort entries by date in the following data: const entries = [ {date: 'lu'}, {date: 'lu'}, {date: 'ma'}, {date: 'ma'} ] I would like the ou ...

Having trouble transmitting JSON data with Durandal JS and Knockout Binding

var newData = { a: ko.observable(), b: ko.observable(), c: ko.observable(), d: ko.observable() }; function setupControlEvents() { $("#save").on("click", handleSave); } function handleSave() { var dataToSen ...

Can you explain the purpose of the .json() function in Angular2?

Can you explain the purpose of the .json() function within http requests in Angular2? Here is an example code snippet: this.http.get('http://localhost:8080/getName') .subscribe(res => this.names = res.json()); Is it correct to assume that t ...

The challenge of maintaining consistency in Vue 3 when it comes to communication between

Context In my Vue 3 application, there is a HomeView component that contains the following template structure: <InputsComponent></InputsComponent> <CheckboxesComponent></CheckboxesComponent> <Toolbar></Toolbar> T ...

Trouble loading CSS file in Vue library from npm package

When using vue-cli to build a library (npm package) that functions for both SSR and client-side, everything seems to be functioning correctly except for one issue; the CSS only loads if the component is present on the page being refreshed. However, when ac ...

Creating a dynamic multi-series line chart in D3 version 4 that incorporates data points with matching colors to their respective lines

In my attempt to enhance a multi-series line chart with D3 V4 in Angular-cli, I have encountered a challenge. Below is the code snippet of what I have been working on. var lookBookData = z.domain().map(function (name) { return { name: name, ...

What could be the reason for the container div's height not being properly refreshed?

When adding elements to a container div with an initial height of 'auto', I assumed that the height would adjust based on the children elements added. However, this is not happening. Can anyone assist me in ensuring that the container div's ...

Error Uploading File to Cloudinary Platform

I am currently developing a REST API using Express.js. The main functionality of the API involves accepting a video file from the client and uploading it to Cloudinary. Interestingly, when I test the API by returning the file back to the client, everything ...

What could be the reason for the absence of 'server' and 'client'?

Trying to retrieve data from db.json but facing some challenges. Below is the content of my db.json file: { "posts": [ { "id": "react-hooks", "title": "React Hooks", "content": "The greatest thing since sliced bread!", "author": "ali" }, ...