-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
refactor default props to fix error message #2452
refactor default props to fix error message #2452
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 93690b0:
|
@andre19980, could you please rebase this PR? |
d3262e8
to
1a7c0d4
Compare
@plouc it worked! Thanks :) |
@andre19980, it seems like the tooltip doesn't work anymore for the choropleth (SVG implementation), see https://nivo.rocks/choropleth/ VS https://nivo-git-fork-andre19980-refactor-default-props-of-b2d03b-plouc.vercel.app/choropleth/. |
A test is also failing for the line chart. |
I hope you guys don't mind, but I saw this PR and decided to take a look. From my investigation it looks like the problem happens when we explicitly pass The failing tests from the line chart explicitly define When we do something like One solution for this problem could be to clear props defined as function getOnlyDefinedProps(props) {
return Object.fromEntries(
Object.entries(props).filter(([_, v]) => v !== undefined)
)
}
// We could also memoize the result, so that the function only runs when props change
function useDefinedProps(props) {
return useMemo(() => getOnlyDefinedProps(props), [props])
}
function Component(props) {
const definedProps = useDefinedProps(props)
const readyToUseProps = {...defaultProps, ...definedProps}
//...
} I hope this helps, as it would fix both the failing tests for the line chart and the tooltip not showing for the choropleth chart. @andre19980 @plouc Let me know if you want me to help with this PR. |
Awesome, @fernandojpps! Thank you for the help! |
You're welcome @andre19980! I understand your point. I actually find it weird that From my understanding, the current behavior with I created a simple React Component to test how it actually behaves, and here are the results. const UndefinedTest = ({name = "John"}) => <p>Hello {name}</p>
<UndefinedTest name={undefined} /> //<p>Hello John</p>
<UndefinedTest /> //<p>Hello John</p>
<UndefinedTest name="Joe" />. //<p>Hello Joe</p> I believe my suggestion helps keep the behavior aligned with the default React behavior, and in turn with developer expectations. |
@andre19980, @fernandojpps, Thank you for your help with this issue, another option would be to explicitly assign a default value for each property needing one, that's what I did for other components, I found it clear enough, even if it's a bit more tedious to maintain, it's easy to see when you have a default or not. That being said, I'm not against the suggested approach as long as it leads to the same default behavior. |
@fernandojpps you're totally right, my bad 🥸 . Hope you don't mind, but I followed @plouc's approach, just cause I also thought it would be more explicit and clearer |
@andre19980 No problem! 🤓 And I agree with you, @plouc's approach makes it easier to read and understand what is going on. |
Any chance this is going to be merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Since there hasn't been made a new release since this was merged, it's not that surprising. @plouc any plans on releasing a new version in the near future? Thanks a lot! |
@nehalist, I'll try to create a new release next week. |
Thanks a lot! 👍 |
When will the new version be released? |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@nivo/axes](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/axes)) | [`0.84.0` -> `0.85.1`](https://renovatebot.com/diffs/npm/@nivo%2faxes/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2faxes/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2faxes/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2faxes/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2faxes/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@nivo/bar](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/bar)) | [`0.84.0` -> `0.85.1`](https://renovatebot.com/diffs/npm/@nivo%2fbar/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2fbar/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2fbar/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2fbar/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2fbar/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@nivo/core](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/core)) | [`0.84.0` -> `0.85.1`](https://renovatebot.com/diffs/npm/@nivo%2fcore/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2fcore/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2fcore/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2fcore/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2fcore/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@nivo/line](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/line)) | [`0.84.0` -> `0.85.1`](https://renovatebot.com/diffs/npm/@nivo%2fline/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2fline/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2fline/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2fline/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2fline/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@nivo/pie](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/pie)) | [`0.84.0` -> `0.85.1`](https://renovatebot.com/diffs/npm/@nivo%2fpie/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2fpie/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2fpie/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2fpie/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2fpie/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@nivo/scatterplot](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/scatterplot)) | [`^0.84.0` -> `^0.85.0`](https://renovatebot.com/diffs/npm/@nivo%2fscatterplot/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2fscatterplot/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2fscatterplot/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2fscatterplot/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2fscatterplot/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | | [@nivo/swarmplot](https://togithub.com/plouc/nivo) ([source](https://togithub.com/plouc/nivo/tree/HEAD/packages/swarmplot)) | [`^0.84.0` -> `^0.85.0`](https://renovatebot.com/diffs/npm/@nivo%2fswarmplot/0.84.0/0.85.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@nivo%2fswarmplot/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@nivo%2fswarmplot/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@nivo%2fswarmplot/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@nivo%2fswarmplot/0.84.0/0.85.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>plouc/nivo (@​nivo/axes)</summary> ### [`v0.85.1`](https://togithub.com/plouc/nivo/releases/tag/v0.85.1) [Compare Source](https://togithub.com/plouc/nivo/compare/v0.85.0...v0.85.1) #### What's Changed - Tooltip: avoid a flash effect and weird initial transition by [@​DimaAmega](https://togithub.com/DimaAmega) in [https://github.com/plouc/nivo/pull/2480](https://togithub.com/plouc/nivo/pull/2480) - feat(deps): upgrade d3-scale due to vulnerability by [@​plouc](https://togithub.com/plouc) in [https://github.com/plouc/nivo/pull/2531](https://togithub.com/plouc/nivo/pull/2531) #### New Contributors - [@​DimaAmega](https://togithub.com/DimaAmega) made their first contribution in [https://github.com/plouc/nivo/pull/2480](https://togithub.com/plouc/nivo/pull/2480) **Full Changelog**: plouc/nivo@v0.85.0...v0.85.1 ### [`v0.85.0`](https://togithub.com/plouc/nivo/releases/tag/v0.85.0) [Compare Source](https://togithub.com/plouc/nivo/compare/v0.84.0...v0.85.0) #### What's Changed - refactor default props to fix error message by [@​andre19980](https://togithub.com/andre19980) in [https://github.com/plouc/nivo/pull/2452](https://togithub.com/plouc/nivo/pull/2452) - fix(sankey): update onClick types in sankey chart to respect generics by [@​stas-demydiuk](https://togithub.com/stas-demydiuk) in [https://github.com/plouc/nivo/pull/2509](https://togithub.com/plouc/nivo/pull/2509) - chore: upgrade d3-color and d3-scale-chromatic by [@​icco](https://togithub.com/icco) in [https://github.com/plouc/nivo/pull/2466](https://togithub.com/plouc/nivo/pull/2466) - Fix: add initial property for truncateTickAt by [@​scalabw](https://togithub.com/scalabw) in [https://github.com/plouc/nivo/pull/2504](https://togithub.com/plouc/nivo/pull/2504) - fix tooltip default color by [@​scalabw](https://togithub.com/scalabw) in [https://github.com/plouc/nivo/pull/2521](https://togithub.com/plouc/nivo/pull/2521) - Touch crosshair for line graphs by [@​WilliamABradley](https://togithub.com/WilliamABradley) in [https://github.com/plouc/nivo/pull/2524](https://togithub.com/plouc/nivo/pull/2524) - fix(marimekko): use readonly arrays for props as the library does not modify them by [@​pcorpet](https://togithub.com/pcorpet) in [https://github.com/plouc/nivo/pull/2493](https://togithub.com/plouc/nivo/pull/2493) - fix(line): use readonly arrays for props as the library does not modify them by [@​pcorpet](https://togithub.com/pcorpet) in [https://github.com/plouc/nivo/pull/2494](https://togithub.com/plouc/nivo/pull/2494) - Fix (peer) dependencies by [@​marvinruder](https://togithub.com/marvinruder) in [https://github.com/plouc/nivo/pull/2528](https://togithub.com/plouc/nivo/pull/2528) #### New Contributors - [@​andre19980](https://togithub.com/andre19980) made their first contribution in [https://github.com/plouc/nivo/pull/2452](https://togithub.com/plouc/nivo/pull/2452) - [@​stas-demydiuk](https://togithub.com/stas-demydiuk) made their first contribution in [https://github.com/plouc/nivo/pull/2509](https://togithub.com/plouc/nivo/pull/2509) - [@​icco](https://togithub.com/icco) made their first contribution in [https://github.com/plouc/nivo/pull/2466](https://togithub.com/plouc/nivo/pull/2466) - [@​scalabw](https://togithub.com/scalabw) made their first contribution in [https://github.com/plouc/nivo/pull/2504](https://togithub.com/plouc/nivo/pull/2504) - [@​WilliamABradley](https://togithub.com/WilliamABradley) made their first contribution in [https://github.com/plouc/nivo/pull/2524](https://togithub.com/plouc/nivo/pull/2524) - [@​marvinruder](https://togithub.com/marvinruder) made their first contribution in [https://github.com/plouc/nivo/pull/2528](https://togithub.com/plouc/nivo/pull/2528) **Full Changelog**: plouc/nivo@v0.84.0...v0.85.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 4am on the first day of the month" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/sebald/pattern-analyzer). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yMzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjIzMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This PR seems to be the likely cause of #2539 . Any chance of someone "here" taking a look? |
hey @GreenAsJade thanks for reporting the issue! So I took a look into the code to see what may be causing the problem and found out that the I was able to fix the warnings by removing the requirement conditions, but I'm not sure if I'm allowed to delete these protections. @plouc I saw in the documentation that many of those props are Btw, I think this problem is also causing the bug #2077, so I think it would solve both problems |
Properties were required because we had defaults, due to how we now define the default values, it won't pass the prop-types validation, I think we should remove the prop-types for the package, as it was done for others, relying on TS instead. |
That would probably be the ideal approach. Most people rely on TS for prop type validation nowadays anyway, and the errors are pretty annoying. |
This PR is related to issue #2415.
Refactor default props declared like:
Now, the default props are defined directly inside the component
The given solution is based in the issue from another repository:
FunctionComp.defaultProps
is going to be deprecated in React 18.3.0 chakra-ui/chakra-ui#7057