Complex Params
Atom params and atom selector params must be serializable (with the exception of passing atom instances themselves). Though it isn't recommended, you can disable this requirement.
How to make Zedux map non-serializable objects to serializable ids when creating atom instance and atom selector ids.
complexParams
This ecosystem config option turns on param mapping for atom and atom selector params.
const ecosystem = createEcosystem({
complexParams: true,
id: 'root',
})
// or
<EcosystemProvider complexParams id="root">
{children}
</EcosystemProvider>
An example passing a filter function as an atom param:
An example with atom selectors:
Try clicking Log > Graph
to see that Zedux generated an id for the getByFilter
atom selector node's param.
Referential Equality
Typically you should extract the complex param into a separate variable so it can be shared across usages:
// these calls create different instances since the function reference changes:
getInstance(numbersAtom, [num => num % 2])
getInstance(numbersAtom, [num => num % 2])
// declare the function separately so it can be reused:
const getEvenNumbers = num => num % 2
// now these calls reuse the same instance:
getInstance(numbersAtom, [getEvenNumbers])
getInstance(numbersAtom, [getEvenNumbers])
Why not?
Using complex params is not recommended because the ids that Zedux generates can make your atom graph more difficult to read and debug. When working with multiple windows or realms of any sort, it also requires more work on your part to share object references across the realms so Zedux can recognize them.