I am trying to create a custom hook which will handle some merging of Aphrodite (https://github.com/Khan/aphrodite) styles for us, but I'm getting some unusual behaviour with the styles object being passed in.
Here's my hook: (the plan was to compose with useMemo which is why it's a hook at all)
import castArray from 'lodash/castArray';
import {StyleSheet} from 'aphrodite';
import merge from 'lodash/merge';
export const useStyles = (styles, props) => {
return {
styles: StyleSheet.create(
[...castArray(styles), ...castArray(props.styles)]
.reduce((agg, next) => (merge(agg, next))),
)
};
};
And basic usage:
function TestComponent(props) {
const {styles} = useStyles(baseStyles, props);
return <div className={css(styles.test)}>Test Text</div>
}
With the idea being that if a styles
prop is passed in, it will be merged with anything in baseStyles and give a final Aphrodite Stylesheet to be used for this instance of the component.
I've created a simple repro repository here: https://github.com/bslinger/hooks-question
Expectation: clicking between the two routes would change the colour of the text, based on whether the props have been passed in to override that style or not.
Actual: once the styles have been merged, even the route without the additional props being passed in shows it with the overridden colour.
Note: I realised that after removing useMemo this wasn't even technically a hook, and downgrading React to 16.7 resulted in the same behaviour, so I guess this is just a Javascript or React question after all?