
Joke components containing jokes. Print these components as you like.Joke component must accept and process the question property, for the main part of the anecdote, and the punchLine property for its key phrase.Joke component can only display the punchLine property punchLine to it, if the question property is not set. Experiment with styling components.index.js file will look quite familiar: import React from "react" import ReactDOM from "react-dom" import App from "./App" ReactDOM.render(<App />, document.getElementById("root")) App.js file: import React from "react" import Joke from "./Joke" function App() { return ( <div> <Joke question="What's the best thing about Switzerland?" punchLine="I don't know, but the flag is a big plus!" /> <Joke question="Did you hear about the mathematician who's afraid of negative numbers?" punchLine="He'll stop at nothing to avoid them!" /> <Joke question="Hear about the new restaurant called Karma?" punchLine="There's no menu: You get what you deserve." /> <Joke question="Did you hear about the actor who fell through the floorboards?" punchLine="He was just going through a stage." /> <Joke question="Did you hear about the claustrophobic astronaut?" punchLine="He just needed a little space." /> </div> ) } export default App Joke component file is located in the same folder as the App component file, we import it with the import Joke from "./Joke" . From the App we return several elements, so the entire output needs to be wrapped in a certain tag, for example, in the <div> . We pass the question and punchLine properties to the instances of the component.Joke.js file: import React from "react" function Joke(props) { return ( <div> <h3>Question: {props.question}</h3> <h3>Answer: {props.punchLine}</h3> <hr/> </div> ) } export default Joke Joke function, we specify the props parameter. Recall that this name is used according to the established tradition. In fact, it can be any, but it is better to call it props .<div> . With the help of the props.question and props.punchLine we refer to the properties passed to the component instance when it is created. These properties become properties of the props object. They are enclosed in braces due to the fact that the JavaScript code used in the JSX markup needs to be in curly braces. Otherwise, the system will accept variable names as plain text. After a pair of <h3> elements, one of which displays the main text of the anecdote, and the other contains its key phrase, there is an <hr/> element describing a horizontal line. These lines will be displayed after each joke, separating them.

Joke component is created, only the punchLine property is punchLine , and the question property is not passed. Creating an instance of such a component looks like this: <Joke punchLine="It's hard to explain puns to kleptomaniacs because they always take things literally." /> App component, the application page will look like the following.
question property is not passed to the component, it displays the text that precedes the main part of each anecdote, after which nothing is output.question property is not passed to the component, the corresponding fragment of the JSX markup returned by it would not be displayed on the page. Here is the complete code of the Joke component, which implements one of the possible approaches to solving our problem with CSS: import React from "react" function Joke(props) { return ( <div> <h3 style={{display: props.question ? "block" : "none"}}>Question: {props.question}</h3> <h3>Answer: {props.punchLine}</h3> <hr/> </div> ) } export default Joke <h3> style, which is defined in the process of creating a component instance based on the presence of the property props.question in the object. If this property is in the object, the element accepts the display: block style and is displayed on the page, if not - display: none and not displayed on the page. In addition, the use of this design will result in: <h3 style={{display: !props.question && "none"}}>Question: {props.question}</h3> display: none style is assigned to the element if the props object props not have a question property, otherwise the display property is not assigned anything.
Joke component look the same. We will think about how to select those of them to which only the punchLine property is punchLine . We solve this problem using the built-in styles, and the approach that we considered above. Here is the updated code for the Joke component: import React from "react" function Joke(props) { return ( <div> <h3 style={{display: !props.question && "none"}}>Question: {props.question}</h3> <h3 style={{color: !props.question && "#888888"}}>Answer: {props.punchLine}</h3> <hr/> </div> ) } export default Joke 
Joke component, it has become more versatile and better suited for reuse.Source: https://habr.com/ru/post/436890/