Skip to content

useBooleanShorthandSyntax strip false values #732

Open
@pascalduez

Description

@pascalduez
Contributor

The useBooleanShorthandSyntax option (which is on by default), strips false values.

While the behavior is fine for a true value: <Foo bar={true}> --> <Foo bar>,
it's not for a false value: <Foo bar={false}> --> <Foo>,
which is definitely not the same thing, the prop is removed completely.

Activity

armandabric

armandabric commented on May 18, 2022

@armandabric
Collaborator

Hi @pascalduez thanks for reporting this case.

I see your point, the actual behavior came from the class component: we could set a default value on the for the props. We use there default value to know if the falsy boolean props should be shown. In that case it works as expected.

Here the class component we use to test this behavior:

class DefaultPropsComponent extends React.Component {}
DefaultPropsComponent.defaultProps = {
test: 'test',
boolean: true,
number: 0,
undefinedProp: undefined,
};

And the actual test:

it('should render boolean props if value is `false`, default is `true` and "showDefaultProps" is false', () => {
expect(
reactElementToJSXString(<DefaultPropsComponent boolean={false} />, {
showDefaultProps: false,
})
).toEqual('<DefaultPropsComponent boolean={false} />');
});

That being said, in the actual react word we could said that most of the components are functional one. We could try to change this behavior to show false props if no default value are set. I will give a try

pascalduez

pascalduez commented on May 18, 2022

@pascalduez
ContributorAuthor

Hey @armandabric,

thanks for your reply.
I'm not sure to fully grasp, but this should be decorrelated from class vs functional components I guess.
You can set "default props" on functional components as well, and in two ways.

  • Wit the defaultProps "static" property
  • With default parameters
rmarquois

rmarquois commented on Jan 24, 2024

@rmarquois

I created a patch from @armandabric PR and it works.
Like @pascalduez said, I think library should handle the two ways to define defaultProps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @armandabric@pascalduez@rmarquois

        Issue actions

          `useBooleanShorthandSyntax` strip false values · Issue #732 · algolia/react-element-to-jsx-string