question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fields: leverage the React Context

See original GitHub issue

Is your feature request related to a problem? Please describe.

ui-kit Fields components use Formik.

We use Formik to handle our form state

However, this usage is not efficient as it involves a lot of props drilling. Let’s take TextField as an example, you can see in the following code that all Formik related props are being repeated explicitly, onChange -> formik.handleChange, etc.

function MyComponent() {
  const formik = useFormikContext();

  <TextField
    name="sku"
    title="SKU"
    value={formik.values.sku}
    errors={formik.errors.sku}
    touched={formik.touched.sku}
    onBlur={formik.handleBlur}
    onChange={formik.handleChange}
  />;
}

This can be slightly improved by using getFieldProps, but it’s still a chore to repeat each time a Field component is used.

function MyComponent() {
  const formik = useFormikContext();

  <TextField
-   name="sku"
    title="SKU"
-    value={formik.values.sku}
-    errors={formik.errors.sku}
-    touched={formik.touched.sku}
-    onBlur={formik.handleBlur}
-    onChange={formik.handleChange}
+    {...formik.getFieldProps('sku')}
  />;
}

Describe the solution you’d like

The solution is already provided by Formik, Leveraging the React Context. The idea is to update ui-kit Fields components to benefit from this solution. This means updating TextField to something similar to the following (copied from the mentioned Formik docs page above):

function TextField(props) {
  // useField() returns [formik.getFieldProps(), formik.getFieldMeta()]
  // which we can spread on <input>. We can use field meta to show an error
  // message if the field is invalid and it has been touched (i.e. visited)
  const [field, meta] = useField(props);
  return (
    <>
      <label htmlFor={props.id || props.name}>{label}</label>
      <input className="text-input" {...field} {...props} />
      {meta.touched && meta.error ? (
        <div className="error">{meta.error}</div>
      ) : null}
    </>
  );
};

This means that finally the usage of TextField will be simplified to the following:

function MyComponent() {
-  const formik = useFormikContext();

  <TextField
    name="sku"
    title="SKU"
-   {...formik.getFieldProps('sku')}
+   // any other non related Formik props
  />;
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ahmehricommented, Dec 22, 2021

Ok thanks for clarifying this.

To be honest, I still don’t see a huge value in having such wrapper components and I believe we can go with useField approach first.

@emmenko I see it makes sense. So what you’re suggesting is a nice solution without introducing trade-offs when introducing new wrapper components. This will avoid us diverging from using ui-kit components, and at the same time allow us to benefit from leveraging the React context using useField.

In this case, I agree and would even consider your solution to be the right way to go. I already flagged the wrapped component that I created to not be used anymore and added a TODO to remove its usages.

Thanks again for your suggestion.

0reactions
emmenkocommented, Dec 22, 2021

Ok thanks for clarifying this.

To be honest, I still don’t see a huge value in having such wrapper components and I believe we can go with useField approach first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context - React
Context. Context provides a way to pass data through the component tree without having to pass props down manually at every level.
Read more >
Using Context API in React with Functional Components
Basically, Context API is designed to share “global” data that can be used in any part of your application without passing props down...
Read more >
Using React Context API with React Hooks for ... - YouTube
Subscribe: https://bit.ly/399gbokLearn how to use React's Context API with React Hooks for State Management in your Applications.
Read more >
React Hooks vs. Redux: Do Hooks and Context replace Redux?
To handle data between disconnected components in React, developers use prop drilling. There is no global state that components can access.
Read more >
React Context API — A Replacement for Redux? | by Rajat S
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In React,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found