0

I want to write clean-code. I have this react-code (typescript 3.6.4):

<Button color="primary"
    size="large"
    disabled={props.user.objectArray.find((element)=> {
                return element.primaryKey === state.findPrimaryKey
            }).isClosed}
    onClick={doSomething}>
Download information

Now I get the message, that the return value could be "undefined". Understandable, maybe the primaryKey could not be found in the object array.

I could write a function which checks first for null, but could this be fixed anyhow without a function?

function getBooleanValue() {
    let booleanCheck = false;
    const temp = props.user.objectArray.find((element) => {
        return element.primaryKey === state.findPrimaryKey
    });
    if (typeof temp !== "undefined") {
        booleanCheck = temp.siteClosed;
    }
    return booleanCheck;
}

How can I write a clean code to get my boolean value?

2 Answers 2

1

I would start by storing the found element into a variable:

const foundElement = props.user.objectArray.find(element => element.primaryKey === state.findPrimaryKey}

Then you can simply fix the condition:

// when element is not found, it's disabled
const isDisabled = !!foundElement && foundElement.isClosed

or

// when element is not found, it's not disabled 
const isDisabled = !foundElement || foundElement.isClosed

or you can use a default value:

const isDisabled = (foundElement || {}).isClosed

In total, the following could work:

(props.user.objectArray.find(element => element.primaryKey === state.findPrimaryKey) || {}).isClosed
Sign up to request clarification or add additional context in comments.

2 Comments

Still get the compiler error "Object is possibly undefined". I think I will stick with the function :-/
looks like a TS bug, the suggested workaround is to use !, the non-null assertion operator => (props.xxx || {})!.isClosed
0

I think you may try this way

<Button color="primary"
    size="large"
    disabled={props.user.objectArray.find((element)=> {
                return element.primaryKey === state.findPrimaryKey
            }) && props.user.objectArray.find((element)=> {
                return element.primaryKey === state.findPrimaryKey
            }).isClosed}
    onClick={doSomething}>
Download information

Here if 1st expression return falsy value then 2nd expression will execute, so if 1st expression is undefined javascript count it as falsy value and then (...).isClosed the 2nd part will not execute

3 Comments

Sadly, compiler is still complaining about the second "props.user.objectarray..." "Object is possibly undefined"
@Paul code should be like this, as props.user.objectArray is returning undefine disabled={props.user.objectArray && props.user.objectArray.find((element)=> { return element.primaryKey === state.findPrimaryKey })}
Maybe it is a compiler error, but Object is possibly undefined on second props.user... I will stay with a function. Thank you for your effort

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.