Pass a function that returns a string in className Reactjs

141
July 03, 2018, at 7:00 PM

I want to pass a function that returns string to set the className of an element by this string in reactjs.

I tried something like this but it didnt return anything:

className={() => shift.spots === 0 ? "notassigned" : "filled"}

Answer 1

As JLRishe pointed out in his comment, you should not use a function but a simple statement instead

className={shift.spots === 0 ? "notassigned" : "filled"}
Answer 2

You need to invoke your function.

    return (
        <div
            className={(() => shift.spots === 0 ? "notassigned" : "filled")()}
        />
    );

Or else you are passing an arrow function to className.

READ ALSO
Can we call Geocoding service from google within a for loop

Can we call Geocoding service from google within a for loop

I have an array of addresses which I need to convert to coordinates lat and long using Google Geocoding service

117
Impossible to link DataBase c#

Impossible to link DataBase c#

It's my first project on C#avec i would like link my DataBase MySQL with my projectI tried with EntityFramework but after choice my DB the windows closed for no reason

112
How can I create remote connection between Toad and MySQL?

How can I create remote connection between Toad and MySQL?

I am trying to connect mysql but not working:

75
background service is used to scan Beacon?

background service is used to scan Beacon?

I can scan the Beacon in foreground using beaconManagerstartScan(); so it is giving the beacons info available in the area

102