简体   繁体   中英

How to set the value of a React component prop using a function?

In one of my React components, I track whether a form is dirty by comparing the hash of a data structure to see if it has changed. Here are the pertinent portions of code (which is non-working):

export const CategoryEdit = (props) => {

    // ... 

    const isDirty = () => {
        return (hash(categoryInfo.data) === dataHash);
    }

    return (
        <>
            <SaveBar isShown={isDirty} />
            <p>...</p>
        </>
    )

}

How can I make it so the SaveBar calls the isDirty function on every render and dynamically shows/hides itself as appropriate? Is this possible?

export const CategoryEdit = (props) => {

// ... 

const isDirty = (dataHash) =>() => {
    return (hash(categoryInfo.data) === dataHash);
}

return (
    <>
        <SaveBar isShown={isDirty(value)} />
        <p>...</p>
    </>
)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM