import PropTypes from "prop-types";
const mycomponent = ({name, children}) =>{
return(
<div>
hello, my name is {name}.<br/>
children value is {children}.
</div>
);
};
mycomponent.defaultProps = {
name : 'basic name'
};
mycomponent.prototype ={
name : PropTypes.string
};
export default mycomponent;
// app.js
// import Mycomponent from "./mycomponent";
//
// const App =()=>{
// return <Mycomponent name = "React">react</Mycomponent>;
// };
//
// export default App;
### `mycomponent` Definition
1. **Importing `PropTypes`**:
```jsx
import PropTypes from "prop-types";
```
This line imports the `PropTypes` library, which is used for type-checking the props passed to a component in React.
2. **Component Definition**:
```jsx
const mycomponent = ({ name, children }) => {
return (
<div>
hello, my name is {name}.<br />
children value is {children}.
</div>
);
};
```
- This is a functional component called `mycomponent`.
- It uses destructuring to extract `name` and `children` from its props.
- The component returns a `div` containing a greeting that includes the `name` prop and displays the `children` prop. The `children` prop is a special prop that lets you pass components or elements as data to other components, essentially nesting them.
3. **Default Props**:
```jsx
mycomponent.defaultProps = {
name: 'basic name'
};
```
- This sets a default value for the `name` prop. If `name` is not specified when `mycomponent` is used, it will default to `"basic name"`.
4. **Prop Types**:
```jsx
mycomponent.prototype = {
name: PropTypes.string
};
```
- This should be `propTypes` instead of `prototype`. This is a mistake in the code. It should be:
```jsx
mycomponent.propTypes = {
name: PropTypes.string
};
```
- This line defines the expected type for the `name` prop using React's `PropTypes`. It specifies that `name` should be a string.
5. **Export**:
```jsx
export default mycomponent;
```
- This makes `mycomponent` available for import in other files.
### `App.js` Usage Example
```jsx
// import Mycomponent from "./mycomponent";
//
// const App = () => {
// return <Mycomponent name="React">react</Mycomponent>;
// };
//
// export default App;
```
- This commented-out code in `App.js` shows how you might use `mycomponent`.
- `Mycomponent` is imported from another file (noting the file path might be incorrect as React component filenames are typically capitalized).
- Inside the `App` component, `Mycomponent` is used with the prop `name` set to `"React"` and the string `"react"` passed as children.
- This setup would render a div saying "hello, my name is React." and "children value is react."
That's the functionality of the given component and its intended usage within an app. Adjusting the `prototype` to `propTypes` would be necessary for proper prop type-checking.
'개념 > React' 카테고리의 다른 글
React) myComponent props value (0) | 2025.04.06 |
---|---|
React) out of value to props (0) | 2025.04.05 |
React) pull out constructor from state (0) | 2025.04.03 |
React) this setState function parameter (0) | 2025.04.02 |
Reat) click the button print out comment value set empty (0) | 2025.04.01 |