App
class code from the App.js
file of the standard React application created by create-react-app
. This code is incomplete, it has errors. import React from "react" class App extends Component() { return ( <div> <h1>{this.state.name}</h1> <h3><font color="#3AC1EF">▍{this.state.age} years old</font></h3> </div> ) } export default App
App
component based on a class does not have a constructor, its state is not initialized, but when forming what it returns, it means that it has a state with some data.return
command, but in class-based components, this command is used in the render()
method, not in the body of the class. Fix it. import React from "react" class App extends Component() { render() { return ( <div> <h1>{this.state.name}</h1> <h3><font color="#3AC1EF">▍{this.state.age} years old</font></h3> </div> ) } } export default App
class App extends Component
construction looks quite normal, with what we refer to as the Component
, there is still something wrong. The problem is that in the import command, import React from "react"
, we import only React
into the project, but not the Component
. That is, we need to either edit this command, bringing it to import React, {Component} from "react"
, then when creating a class we can use the already existing code, or rewrite the class declaration in this form: class App extends React.Component
. We will focus on the first option. Now the component code looks like this: import React, {Component} from "react" class App extends Component() { render() { return ( <div> <h1>{this.state.name}</h1> <h3><font color="#3AC1EF">▍{this.state.age} years old</font></h3> </div> ) } } export default App
TypeError: Cannot set property 'props' of undefined
error message appears in the browser TypeError: Cannot set property 'props' of undefined
, we are informed that something is wrong with the first line of the class declaration.TypeError: Cannot read property 'name' of null
. Obviously, it refers to an attempt to use data stored in a component state that has not yet been initialized. Therefore, now we will create a class constructor, not forgetting to call super()
in it, and initialize the state of the component, adding to it the values that the component is trying to work with.App
component: import React, {Component} from "react" class App extends Component { constructor() { super() this.state = { name: "Sally", age: 13 } } render() { return ( <div> <h1>{this.state.name}</h1> <h3><font color="#3AC1EF">▍{this.state.age} years old</font></h3> </div> ) } } export default App
isLoggedIn
property should be present, storing the boolean value true
if the user is logged in, and false
if not (in our case there are no “logon" mechanisms here, the corresponding value must be set manually when the state is initialized).You are currently logged in
text You are currently logged in
, and if not, then the text You are currently logged out
. This task is optional, if you have difficulties in carrying it out, you can, for example, search for ideas on the Internet.App.js
file: import React from "react" function App() { return ( <div> <h1>You are currently logged (in/out)</h1> </div> ) } export default App
import React from "react" class App extends React.Component { constructor() { super() this.state = { isLoggedIn: true } } render() { return ( <div> <h1>You are currently logged (in/out)</h1> </div> ) } } export default App
in
or out
string, depending on what is stored in the isLoggedIn
state isLoggedIn
. We will work with this variable in the render()
method, first analyzing the data and writing down the desired value into it, and then using it in the JSX code returned by the component. This is what we ended up with: import React from "react" class App extends React.Component { constructor() { super() this.state = { isLoggedIn: true } } render() { let wordDisplay if (this.state.isLoggedIn === true) { wordDisplay = "in" } else { wordDisplay = "out" } return ( <div> <h1>You are currently logged {wordDisplay}</h1> </div> ) } } export default App
wordDisplay
variable is a regular local variable declared in the render()
method, so to access it within this method, you just need to specify its name.isLoggedIn
state isLoggedIn
set to true
, so the page displays the text You are currently logged in
. To display the text You are currently logged out
you need to change, in the component code, the isLoggedIn
value to false
.isLoggedIn
is a logical variable, the if (this.state.isLoggedIn === true)
condition if (this.state.isLoggedIn === true)
can be rewritten as if (this.state.isLoggedIn)
.Source: https://habr.com/ru/post/438988/