|
| 1 | +import { useState } from 'react'; |
| 2 | + |
| 3 | +// // Class components are old and no longer recommended |
| 4 | +// class ExampleClassToFunctionClass extends React.Component { |
| 5 | +// state = { |
| 6 | +// detailsShown: false, |
| 7 | +// }; |
| 8 | + |
| 9 | +// render() { |
| 10 | +// const { user } = this.props; |
| 11 | + |
| 12 | +// if (user === undefined) return <div>User not found!</div>; |
| 13 | + |
| 14 | +// return ( |
| 15 | +// <> |
| 16 | +// <img src={user.image} alt={user.imageAlt} /> |
| 17 | +// <h1>{user.name}</h1> |
| 18 | +// <button |
| 19 | +// onClick={() => |
| 20 | +// this.setState({ detailsShown: !this.state.detailsShown }) |
| 21 | +// } |
| 22 | +// > |
| 23 | +// {this.state.detailsShown ? 'Hide' : 'Show'} Details |
| 24 | +// </button> |
| 25 | +// {this.state.detailsShown && <p>{user.details}</p>} |
| 26 | +// </> |
| 27 | +// ); |
| 28 | +// } |
| 29 | +// } |
| 30 | + |
| 31 | +function ExampleClassToFunctionClass(props) { |
| 32 | + const [detailsShown, setDetailsShown] = useState(false); |
| 33 | + |
| 34 | + if (props.user === undefined) return <div>User not found!</div>; |
| 35 | + |
| 36 | + return ( |
| 37 | + <> |
| 38 | + <img src={props.user.image} alt={props.user.imageAlt} /> |
| 39 | + <h1>{props.user.name}</h1> |
| 40 | + <button onClick={() => setDetailsShown((prev) => !prev)}> |
| 41 | + {detailsShown ? 'Hide' : 'Show'} Details |
| 42 | + </button> |
| 43 | + {detailsShown && <p>{props.user.details}</p>} |
| 44 | + </> |
| 45 | + ); |
| 46 | +} |
| 47 | + |
| 48 | +export default function ExampleConvertingClassComponentsToFunctionComponents() { |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + <h1>ExampleConvertingClassComponentsToFunctionComponents</h1> |
| 52 | + <ExampleClassToFunctionClass |
| 53 | + user={{ |
| 54 | + details: 'I like memes', |
| 55 | + name: 'Lukas', |
| 56 | + imageAlt: 'this is an image', |
| 57 | + image: |
| 58 | + 'https://api.memegen.link/images/buzz/memes/memes_everywhere.png?width=400', |
| 59 | + }} |
| 60 | + /> |
| 61 | + </div> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +// If you wanted to learn about class components, you could use a ChatGPT / Gemini / Claude prompt like this: |
| 66 | + |
| 67 | +// I have a React class component that I would like to convert to a function component: |
| 68 | +// |
| 69 | +// ``` |
| 70 | +// class ExampleClassToFunctionClass extends React.Component { |
| 71 | +// state = { |
| 72 | +// detailsShown: false, |
| 73 | +// }; |
| 74 | +// |
| 75 | +// render() { |
| 76 | +// const { user } = this.props; |
| 77 | +// |
| 78 | +// if (user === undefined) return <div>User not found!</div>; |
| 79 | +// |
| 80 | +// return ( |
| 81 | +// <> |
| 82 | +// <img src={user.image} alt={user.imageAlt} /> |
| 83 | +// <h1>{user.name}</h1> |
| 84 | +// <button |
| 85 | +// onClick={() => |
| 86 | +// this.setState({ detailsShown: !this.state.detailsShown }) |
| 87 | +// } |
| 88 | +// > |
| 89 | +// {this.state.detailsShown ? 'Hide' : 'Show'} Details |
| 90 | +// </button> |
| 91 | +// {this.state.detailsShown && <p>{user.details}</p>} |
| 92 | +// </> |
| 93 | +// ); |
| 94 | +// } |
| 95 | +// } |
| 96 | +// ``` |
| 97 | +// |
| 98 | +// I am learning about class components. Can you guide me through (without telling me the answer) and show me how to convert it myself? Work step by step and ask me lots of questions. |
0 commit comments