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

refactor default props to fix error message #2452

Merged

Conversation

andre19980
Copy link
Contributor

@andre19980 andre19980 commented Oct 20, 2023

This PR is related to issue #2415.

Refactor default props declared like:

Component.defaultProps = {
    prop1: 0,
    prop2: 1,
    // ....
}

Now, the default props are defined directly inside the component

export const Component = props => {
    const {
      prop1 = 0,
      prop2 = 1,
      // ...
    } = props
    // ...
}

The given solution is based in the issue from another repository:

@vercel
Copy link

vercel bot commented Oct 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nivo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 1, 2023 2:41am

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 20, 2023

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:

Sandbox Source
nivo Configuration

@plouc
Copy link
Owner

plouc commented Nov 14, 2023

@andre19980, could you please rebase this PR?

@andre19980
Copy link
Contributor Author

@plouc it worked! Thanks :)

@plouc
Copy link
Owner

plouc commented Nov 14, 2023

@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/.

@plouc
Copy link
Owner

plouc commented Nov 14, 2023

A test is also failing for the line chart.

@ofps
Copy link

ofps commented Nov 28, 2023

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 undefined as a prop.

The failing tests from the line chart explicitly define axisLeft and axisBottom as undefined. If we comment those lines all tests pass but this actually reveals a bigger issue.

When we do something like {...defaultProps, ...props} we are overriding all props that are defined in props, even if they are defined as undefined 👀.
As an example of this behaviour, you can check that { ...{ a: 1 }, ...{ a: undefined} } evaluates to {a: undefined}.

One solution for this problem could be to clear props defined as undefined before using them, like:

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.

@andre19980
Copy link
Contributor Author

Awesome, @fernandojpps! Thank you for the help!
Your solution seems to fix the failing test, but I wonder if filtering the received props is actually the right behavior, 'cause maybe the person using the component wants to send an undefined prop. In this case, the component shouldn't discard it, especially if it's gonna affect the component's behavior, like prevent something from being rendered. Then I guess we would be creating a bug.

@ofps
Copy link

ofps commented Nov 29, 2023

You're welcome @andre19980!

I understand your point. I actually find it weird that undefined can have two different meanings.

From my understanding, the current behavior with defaultProps is to use default values whenever a prop is undefined. It doesn't matter if it was explicitly set as undefined or not.

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.

@plouc
Copy link
Owner

plouc commented Nov 29, 2023

@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.

@andre19980
Copy link
Contributor Author

@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

@ofps
Copy link

ofps commented Dec 1, 2023

@andre19980 No problem! 🤓
I'm glad we figured out what the problem was and found a way to fix it!

And I agree with you, @plouc's approach makes it easier to read and understand what is going on.

@nehalist
Copy link

nehalist commented Jan 7, 2024

Any chance this is going to be merged?

Copy link
Owner

@plouc plouc left a comment

Choose a reason for hiding this comment

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

LGTM.

@plouc plouc merged commit d87af09 into plouc:master Jan 7, 2024
5 checks passed
@nehalist
Copy link

I'm still getting the error

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!

@plouc
Copy link
Owner

plouc commented Jan 13, 2024

@nehalist, I'll try to create a new release next week.

@nehalist
Copy link

Thanks a lot! 👍

@b310L
Copy link

b310L commented Jan 23, 2024

When will the new version be released?

@nehalist nehalist mentioned this pull request Feb 10, 2024
sebald referenced this pull request in sebald/pattern-analyzer Mar 10, 2024
[![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 (@&#8203;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
[@&#8203;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
[@&#8203;plouc](https://togithub.com/plouc) in
[https://github.com/plouc/nivo/pull/2531](https://togithub.com/plouc/nivo/pull/2531)

#### New Contributors

- [@&#8203;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
[@&#8203;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 [@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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 [@&#8203;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 [@&#8203;pcorpet](https://togithub.com/pcorpet) in
[https://github.com/plouc/nivo/pull/2494](https://togithub.com/plouc/nivo/pull/2494)
- Fix (peer) dependencies by
[@&#8203;marvinruder](https://togithub.com/marvinruder) in
[https://github.com/plouc/nivo/pull/2528](https://togithub.com/plouc/nivo/pull/2528)

#### New Contributors

- [@&#8203;andre19980](https://togithub.com/andre19980) made their first
contribution in
[https://github.com/plouc/nivo/pull/2452](https://togithub.com/plouc/nivo/pull/2452)
- [@&#8203;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)
- [@&#8203;icco](https://togithub.com/icco) made their first
contribution in
[https://github.com/plouc/nivo/pull/2466](https://togithub.com/plouc/nivo/pull/2466)
- [@&#8203;scalabw](https://togithub.com/scalabw) made their first
contribution in
[https://github.com/plouc/nivo/pull/2504](https://togithub.com/plouc/nivo/pull/2504)
- [@&#8203;WilliamABradley](https://togithub.com/WilliamABradley) made
their first contribution in
[https://github.com/plouc/nivo/pull/2524](https://togithub.com/plouc/nivo/pull/2524)
- [@&#8203;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>
@GreenAsJade
Copy link

This PR seems to be the likely cause of #2539 . Any chance of someone "here" taking a look?

@andre19980
Copy link
Contributor Author

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 Line component has its props defined with isRequired in the PropTypes declaration. One can check in the file packages/line/src/props.js

Captura de Tela 2024-03-19 às 12 20 35

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 optional, so I'm a bit confused if they should be marked with isRequired. To solve the warnings, I was thinking of editing the components where I did the refactor in the default props and remove the isRequired condition from the props which are written as optional in the documentation. Would that be ok and safe?

Btw, I think this problem is also causing the bug #2077, so I think it would solve both problems

@plouc
Copy link
Owner

plouc commented Mar 19, 2024

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.
I removed the prop-types for most packages when I migrated them to TS, @nivo/line hasn't been migrated yet, but we could still remove them.

@csandman
Copy link

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. I removed the prop-types for most packages when I migrated them to TS, @nivo/line hasn't been migrated yet, but we could still remove them.

That would probably be the ideal approach. Most people rely on TS for prop type validation nowadays anyway, and the errors are pretty annoying.

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.

7 participants