how to create state in class component
const [state, setState] = useState('intialValue')
how to create state in class component
const [state, setState] = useState('intialValue')
class component react using state
import React, {Component} from 'react';
import './App.css';
import IconBar from './components/icon-bar';
import Events from './components/events';
class App() extends Component {
constructor(props) {
super(props)
this.state = {
mode: null
}
this.updateMode = this.updateMode.bind(this);
}
updateMode = (newMode) => {
this.setState({mode: newMode});
}
return (
<div className="App">
<h1 className="Title">ENAKS</h1>
<IconBar onUpdateMode={this.updateMode} mode={this.state.mode} />
<Events />
<div className="mainModeFrame"><p1>{this.state.mode}</p1></div>
</div>
);
}
export default App;
Add this to your knowledge
set state
class App extends React.Component {
state = { count: 0 }
handleIncrement = () => {
this.setState({ count: this.state.count + 1 })
}
handleDecrement = () => {
this.setState({ count: this.state.count - 1 })
}
render() {
return (
<div>
<div>
{this.state.count}
</div>
<button onClick={this.handleIncrement}>Increment by 1</button>
<button onClick={this.handleDecrement}>Decrement by 1</button>
</div>
)
}
}
state react
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
count: 0
};
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Click me
</button>
</div>
);
}
}
state in react
class BalanceInquiry extends Component {
constructor(props) {
super(props);
this.state = {
totalIncome: 0,
};
}
render() {
return <div>totalIncome {this.state.totalIncome}</div>;
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us