Really you are starting, the state of the component as it was created will not work, it is isolated to only one location and in it can have several states in the case so exemplify:
state = {dataLoad: '', isHungry: ''};
and to change their states may be individual:
this.setState({dataLoad: true});
or together:
this.setState({dataLoad: true, isHungry: false});
however I decided to exemplify a simple component:
class Cursos extends React.Component {
state = {dataLoad: '', isHungry: '' };
componentDidMount(){
this.setState({dataLoad: true, isHungry: false});
}
render() {
return (
<div>
<div>{new String(this.state.dataLoad)}</div>
<div>{new String(this.state.isHungry)}</div>
</div>
)
}
}
ReactDOM.render(<Cursos/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
has also with hooks
or Functional Components, as in this example:
function Cursos() {
const [dataLoad, setDataLoad] = React.useState('');
const [isHungry, setIsHungry] = React.useState('');
React.useEffect(() => {
setDataLoad(true);
setIsHungry(false);
}, []);
return (
<div>
<div>{new String(dataLoad)}</div>
<div>{new String(isHungry)}</div>
</div>
)
}
ReactDOM.render(<Cursos/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
He’s using the class concept, so that way it won’t work.
– Rhadamez Gindri Hercilio
If its React is up-to-date it will work. The updated React has support for classes and functions. The Hooks came to improve.
– Jr Bytes
Thank you, your solution was very good
– Alderjanio de oliveira almeida