Is it okay to use redux's initialState to check if data has changed

225 views Asked by At

Inside my functional component i need to check if sort order was changed and if it was i need to highlight a sort button. Default order is described inside a reducer's initialState. Currently i'm importing this "initialState" const and comparing a field from that to a field inside the useSelector's state. It looks like that:

import { initialState as initialStateSettings } from 'Reducers/settings'

const sortButton = () => {

    const isOrderChanged = useSelector((state) => {
        const defaultOrder = initialStateSettings.params.order
        return !compareRatesOrder(state.settings.params.order, defaultOrder)
    })

    return <button className={isOrderChanged ? 'gray-button' : 'blue-button'} >Sort</button>
}

is it an acceptable way of doing it? Because it doesn't look elegant. And if it's wrong, what is the best way of checking is state data differs from initialState.

Currently I use redux without redux-toolkit.

1

There are 1 answers

0
phry On

Generally there speaks nothing against doing that.

As for using Redux without RTK: you should really be using RTK. It's the official recommendation since 2019, even for apps existing before that. Please read why Redux Toolkit is how to use Redux today