index.js
file of a standard React application created by create-react-app
. Convert the functional components you find in this code into class-based components, and also find and fix a small error.index.js
: import React from "react" import ReactDOM from "react-dom" // #1 function App() { return ( <div> <Header /> <Greeting /> </div> ) } // #2 function Header(props) { return ( <header> <p>Welcome, {props.username}!</p> </header> ) } // #3 function Greeting() { const date = new Date() const hours = date.getHours() let timeOfDay if (hours < 12) { timeOfDay = "morning" } else if (hours >= 12 && hours < 17) { timeOfDay = "afternoon" } else { timeOfDay = "night" } return ( <h1>Good {timeOfDay} to you, sir or madam!</h1> ) } ReactDOM.render(<App />, document.getElementById("root"))
Header
component, expecting to get the username
property that was set when creating its instance. An instance of this component is created in the App
component. Having found this out, we can correct the very mistake that was mentioned in the assignment.App
into a component based on a class. To do this, it is enough to bring its code to this form: class App extends React.Component { render() { return ( <div> <Header username="vschool"/> <Greeting /> </div> ) } }
class
name is now in front of it, the extends React.Component
command is followed by the extends React.Component
, after which, in curly brackets, the class body is described. There must be a render()
method that returns what we returned from the functional component. Other components are processed in the same way. Notice the <Header username="vschool"/>
construct. Here we pass to the Header
component the username
property with the value of vschool
, thereby correcting the error that exists in the source application.Header
component expects to get the username
property, and in a functional component, access to this property is performed using the props.username
construction ( props
in this case is an argument of the function describing the component). In class-based components, the same looks like this.props.username
. Here is the revised code for the Header
component: class Header extends React.Component { render() { return ( <header> <p>Welcome, {this.props.username}!</p> </header> ) } }
Greeting
, is a bit different. The fact is that in it, before the return
command, some calculations are performed. When converting it to a component based on a class, these calculations must be placed in the render()
method before the return command. Here is the code for the reworked Greeting
component: class Greeting extends Component { render() { const date = new Date() const hours = date.getHours() let timeOfDay if (hours < 12) { timeOfDay = "morning" } else if (hours >= 12 && hours < 17) { timeOfDay = "afternoon" } else { timeOfDay = "night" } return ( <h1>Good {timeOfDay} to you, sir or madam!</h1> ) } }
class Greeting extends Component
. Often they do this for the sake of brevity of code, but in order for this to work, we need to refine the import command react
, putting it into this form: import React, {Component} from "react"
Header
component.index.js
file: import React, {Component} from "react" import ReactDOM from "react-dom" // #1 class App extends React.Component { render() { return ( <div> <Header username="vschool"/> <Greeting /> </div> ) } } // #2 class Header extends React.Component { render() { return ( <header> <p>Welcome, {this.props.username}!</p> </header> ) } } // #3 class Greeting extends Component { render() { const date = new Date() const hours = date.getHours() let timeOfDay if (hours < 12) { timeOfDay = "morning" } else if (hours >= 12 && hours < 17) { timeOfDay = "afternoon" } else { timeOfDay = "night" } return ( <h1>Good {timeOfDay} to you, sir or madam!</h1> ) } } ReactDOM.render(<App />, document.getElementById("root"))
this.props.name = "NoName"
- we will encounter an error message.App.js
file of a standard project created by create-react-app
: import React from "react" class App extends React.Component { render() { return ( <div> <h1>Is state important to know?</h1> </div> ) } } export default App
constructor()
class method. After this, the component code will look like this: class App extends React.Component { constructor() { } render() { return ( <div> <h1>Is state important to know?</h1> </div> ) } } Constructor()
constructor()
method.super()
function. In the constructor of the parent class, some initialization operations can be performed, the results of which will be useful to our object. Here is what the constructor of our class will now look like: constructor() { super() }
state
property to the class instance. This property is an object: constructor() { super() this.state = {} }
this.state
construction. Add a new property to the state: constructor() { super() this.state = { answer: "Yes" } }
Is state important to know?
. The state stores the answer to this question. In order to add this answer after the question, you need to do the same as we usually do by adding JavaScript constructs to the JSX code. Namely, you need to add the {this.state.answer}
construct to the end of the line. As a result, the complete component code will look like this: class App extends React.Component { constructor() { super() this.state = { answer: "Yes" } } render() { return ( <div> <h1>Is state important to know? {this.state.answer}</h1> </div> ) } }
ChildComponent
, the data from the state can be passed to it like this: <ChildComponent answer={this.state.answer}/>
setState()
method, which is used to solve this problem, not only the state of the component will be changed, but also the state data passed through the property mechanism to its child components will be updated. In addition, a state change will cause the data from the state displayed on the application page to change automatically.Source: https://habr.com/ru/post/438986/