Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Add useEditController hook #3398

Merged
merged 8 commits into from
Jul 12, 2019
Merged

[RFR] Add useEditController hook #3398

merged 8 commits into from
Jul 12, 2019

Conversation

fzaninotto
Copy link
Member

Removes one level of indentation in the React tree for Edit views.

I also introduced a new specialized mutation hook, useUpdate. This addresses #2952 for the crudUpdate action.

import { useEditController } from 'react-admin';
import EditView from './EditView';

const MyEdit = props => {
     const controllerProps = useEditController(props);
     return <EditView {...controllerProps} {...props} />;
}

The only difference between useEditController and EditController is that the former doesn't inject translate (just like for useListController).

@fzaninotto fzaninotto added this to the 3.0.0 milestone Jul 10, 2019
Copy link
Collaborator

@djhi djhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

packages/ra-core/package.json Outdated Show resolved Hide resolved

useEffect(() => {
dispatch(resetForm(REDUX_FORM_NAME));
}, [resource, id, version]); // eslint-disable-line react-hooks/exhaustive-deps
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should pass the missing deps

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the previous code was:

    componentWillReceiveProps(nextProps: Props & EnhancedProps) {
        if (
            this.props.id !== nextProps.id ||
            nextProps.version !== this.props.version
        ) {
            this.props.resetForm(REDUX_FORM_NAME);
            this.updateData(nextProps.resource, nextProps.id);
        }
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fail to see how it relates to the new hook version and to what hooks actually requires

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just read this from an article by Dan:

You may omit dispatch, setState, and useRef container values from the deps because React guarantees them to be static. But it also doesn’t hurt to specify them.)

https://overreacted.io/a-complete-guide-to-useeffect/

Maybe it's true for react-redux dispatch as well

packages/ra-core/src/fetch/useMutation.ts Show resolved Hide resolved
@djhi djhi merged commit 77376cf into next Jul 12, 2019
@djhi djhi deleted the useEditController branch July 12, 2019 11:01
@fzaninotto fzaninotto mentioned this pull request Jul 14, 2019
40 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants