OK, I was a bit too fast in stating that 2.0.0 fixed my issue. My goal was to intercept setModel
, not setProperty
and it's still impossible to override. Now that I understand what is happening, it's easy to see why: if you don't override setProperty
, it will still be "locked in" with the original setModel
function.
This for example will not work.
const middleware = function(props) {
const { setModel, ...xProps } = props
const interceptedSetModel = (model)=> {
console.log("Intercepting model")
console.log(model)
return setModel(model)
}
return { setModel: interceptedSetModel, ...xProps }
}
const SimpleForm = props => {
const { bindInput } = props
return (
<Form>
<Input {...bindInput('example')} />
</Form>
)
}
const ReformedForm = reformed(middleware)(SimpleForm)
Now, you may begin to wonder why we would want to use that?
Let's say we're creating a dynamic form that has "repeatable" parts. The output we are aiming for is an array like this:
output = [
{
name: "foo",
value: "hello"
},
{
name: "bar",
value: "world"
},
]
If I can intercept setModel
, I can easily store the state of the entire "form" into an array inside of a higher order state object. We could perhaps do this with setProperty
as well, but I'm betting this will break bindInput
, etc because the internal state of the "lower order" reformed component will not be updated anymore...