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

optimization of field render is missing some cache keys #8

Closed
WangLarry opened this issue Oct 27, 2021 · 1 comment · Fixed by #9
Closed

optimization of field render is missing some cache keys #8

WangLarry opened this issue Oct 27, 2021 · 1 comment · Fixed by #9
Assignees
Labels
bug Something isn't working

Comments

@WangLarry
Copy link

WangLarry commented Oct 27, 2021

export default function createField(FieldView) {

  const Field = ({ id, ...customProps }) => {
    const [cachedProps, setCachedProps] = useState();
    const [cachedFieldView, setCachedFieldView] = useState((null));
    const { model, resources, actions } = useContext(FormContext);
    
    const props = mapFieldToProps(id, model, resources);
  
    // optimization prevent un-necessary field renders
    if (!isEqual(props, cachedProps)) {
      setCachedProps(props);
      setCachedFieldView(<FieldView
        {...customProps}
        {...props}
        onValueChange={value => actions.changeValue(id, value)}
        onStateChange={state => actions.changeState(id, state)}
      />);
    }

    return cachedFieldView;
  };
  
  Field.propTypes = {
    id: PropTypes.string.isRequired,
  };

  return Field;
}

if customProps or actions are changed, filed is not re-render.

@galhavivi galhavivi added the bug Something isn't working label Oct 27, 2021
@galhavivi
Copy link
Owner

About custom props - will be fixed, thank you for noticing :)

About actions changed - thats not a case, since the "changeValue" which is passed to the FieldView in an internal changeValue function of Form.js - the function which handles the change value lifecycle (its not an input from the user)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants