Home > database >  Why 'name' is not passed to React component?
Why 'name' is not passed to React component?

Time:07-20

Is 'name' parameter unique in any way, why it is not passed?

I have a componenet called Input. This is the interface for it:

interface Props {
    className?: string
    colour?: ThemeColour
    multiLine?: boolean
    keyPath?: (string | number)[]
    onChangePrio?: Function
    onBlurPrio?: Function
    valuePrio?: any
    isInt?: Boolean
    isFloat?: Boolean
    formState?: any
    setFormState?: Function
    name?: string
}
const Input = ({
    className = '',
    multiLine = false,
    keyPath,
    onChangePrio,
    onBlurPrio,
    valuePrio,
    isInt,
    isFloat,
    formState,
    setFormState,
    name,
    ...rest
}: Props) => {
    console.log('n1', name)

and here it is called:

<InputGroup title="Egyéb információ">
<Input
    formState={formState}
    setFormState={setFormState}
    name="additionalInfo"
    placeholder="pl. Szállással"
/>
</InputGroup>

Is it a problem if optional parameters as not set as undefined or null?

I see only these repeating lines:

n1 undefined
n1 undefined
n1 undefined
n1 undefined
n1 undefined
n1 undefined
n1 undefined
n1 undefined

Why, though name is passed at least in one case.

CodePudding user response:

No, of course it isn't a problem. Any props you haven't defined when using the component will just be undefined within the component (unless overridden by e.g. defaultProps).

function Greet({ name }: { name?: string }) {
  return <div>Hello, {String(name)}</div>;
}

export default function App() {
  return (
    <div className="App">
      <Greet name="János" />
      <Greet />
    </div>
  );
}

will render

Hello, János
Hello, undefined
  • Related