I want the show Input
component name
props value.
I mean component props name, not component name.
<Input
type="radio"
id="radio-input-1"
name="radio-inupt"
value="Mango"
/>
<span>Input componet name is {Input.name}</span>
Join Stack Overflow to learn, share knowledge, and build your career.
I want the show Input
component name
props value.
I mean component props name, not component name.
<Input
type="radio"
id="radio-input-1"
name="radio-inupt"
value="Mango"
/>
<span>Input componet name is {Input.name}</span>
How i can show React component props value?
Components don't have prop values. A component is like a constructor, prop values are like arguments passed to the constructor. Since you can have a multiple "instances" of a component, the component itself cannot be associated with a specific set of prop values.
Instead, when you use <Input ... />
you are actually calling React.createElement(Input, ...)
. This returns an element (an "instance" of the component) and it's the element that is associated with the concrete prop values.
The shape of an element is not officially documented (at least not on the website), so accessing it is likely not something you should do, but if you really want to you can access the props
property of the element:
function Input() {}
var input = <Input
type="radio"
id="radio-input-1"
name="radio-inupt"
value="Mango"
/>;
console.log(`Input element name is ${input.props.name}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<Input name="foo" />.props.name === 'foo' // true
– rlemon Mar 12 '19 at 19:00