// const mycomponent = props =>{
// const {name, children} = props;
// return(
// <div>
// hello. my name is {name}.<br/>
// children value is {children}.
// </div>
// );
// };
//
// mycomponent.defaultProps = {
// name : 'basic name'
// };
//
// export default mycomponent;
const mycomponent = ({name, children}) =>{
return(
<div>
hello, my name is {name}.<br/>
children value is {children}.
</div>
);
};
mycomponent.defaultProps = {
name : 'basic name'
};
export default mycomponent;
### Functional Component with Destructuring Props
- **Destructuring Props**: The component receives `props` as an argument, and destructuring is used directly in the parameter list to extract `name` and `children`. This is a concise way to access props, making the function body cleaner and more readable.
### Rendering and Props Usage
- **JSX Rendering**: The component returns JSX, which includes the `name` prop in a greeting message and displays the `children` prop content. The `children` prop is a special prop automatically passed by React to components, representing any child elements defined within the component's opening and closing tags.
### Default Props
- **Default Prop for `name`**: The `defaultProps` static property is set on `mycomponent`, specifying a default value for the `name` prop. If `mycomponent` is used without providing a `name`, it defaults to `'basic name'`. This feature ensures that the component behaves as expected even when certain props are not provided.
### Exporting the Component
- **Export Statement**: The component is exported at the end of the file using `export default mycomponent;`. This allows it to be imported and used in other parts of your application.
### Example Usage
Here's an example of how you might use `mycomponent` in another component:
```jsx
import React from 'react';
import MyComponent from './MyComponent'; // Adjust the path as needed
function App() {
return (
<MyComponent name="John Doe">
<p>This is a child component or content passed as children prop.</p>
</MyComponent>
);
}
export default App;
```
### Suggestion
While the code is correctly written and follows React's principles for props and component structure, adopting the convention of naming components in PascalCase (`MyComponent`) would align your code with common React practices, improving readability and maintainability.
'개념 > React' 카테고리의 다른 글
React) mycomponent props children (0) | 2025.04.07 |
---|---|
React) myComponent props value (0) | 2025.04.06 |
React) propTypes (0) | 2025.04.04 |
React) pull out constructor from state (0) | 2025.04.03 |
React) this setState function parameter (0) | 2025.04.02 |