Skip to content

Commit

Permalink
Support events with shallow deps (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson authored Jan 17, 2025
1 parent 429adfe commit aa9fa76
Showing 1 changed file with 4 additions and 35 deletions.
39 changes: 4 additions & 35 deletions src/r3f/utilities/useOptions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function setValueAtPath( object, path, value ) {
}

// Recursively assigns a set of options to an object, interpreting dashes as periods
export function useDeepOptions( target, options ) {
export function useDeepOptions( target, options, shallow = false ) {

// assign options recursively
useEffect( () => {
Expand All @@ -71,7 +71,7 @@ export function useDeepOptions( target, options ) {

} else {

const path = getPath( key );
const path = shallow ? [ key ] : getPath( key );
previousState[ key ] = getValueAtPath( target, path );
setValueAtPath( target, path, options[ key ] );

Expand All @@ -93,7 +93,7 @@ export function useDeepOptions( target, options ) {

for ( const key in options ) {

const path = getPath( key );
const path = shallow ? [ key ] : getPath( key );
setValueAtPath( target, path, options[ key ] );

}
Expand All @@ -107,37 +107,6 @@ export function useDeepOptions( target, options ) {
// Assigns a set of options to an object shallowly, interpreting dashes as periods
export function useShallowOptions( instance, options ) {

// assigns any provided options to the plugin
useEffect( () => {

if ( instance === null ) {

return;

}

const previousState = {};
for ( const key in options ) {

if ( key in instance ) {

previousState[ key ] = instance[ key ];
instance[ key ] = options[ key ];

}

}

return () => {

for ( const key in previousState ) {

instance[ key ] = previousState[ key ];

}

};

}, [ instance, useObjectDep( options ) ] ); // eslint-disable-line
useDeepOptions( instance, options, true );

}

0 comments on commit aa9fa76

Please sign in to comment.