From React 16.8.0 You can use Hooks using useState to instantiate a State custom in your Functional Component. Like This...
import React, {useState} from 'react';
const AddButon = ({handleAddValue}) => {
return <button onClick={handleAddValue}>Add</button>
}
const App = (props) =>{
const [value, setValue] = useState(0);
const handleAddValue = () => {
const newValue = value+1;
setValue(newValue);
}
return (
<div>
<div>The Value is: {value}</div>
<AddButon handleAddValue={handleAddValue} />
</div>);
}
If you want to read more about this new functionality, follow the following link.
https://reactjs.org/docs/hooks-intro.html
Answer from Rafael Maldonado on Stack OverflowFrom React 16.8.0 You can use Hooks using useState to instantiate a State custom in your Functional Component. Like This...
import React, {useState} from 'react';
const AddButon = ({handleAddValue}) => {
return <button onClick={handleAddValue}>Add</button>
}
const App = (props) =>{
const [value, setValue] = useState(0);
const handleAddValue = () => {
const newValue = value+1;
setValue(newValue);
}
return (
<div>
<div>The Value is: {value}</div>
<AddButon handleAddValue={handleAddValue} />
</div>);
}
If you want to read more about this new functionality, follow the following link.
https://reactjs.org/docs/hooks-intro.html
Update: As of React 16.8.0, this is no longer true. See Rafael Moldonado's answer above
Stateless Functional Components can't have state, you will need to use a regular React Component if you want to have state within the component.
React: How to use setState inside functional component?
reactjs - Usage of State in functional component of React - Stack Overflow
reactjs - How to use state in this functional component? - Stack Overflow
Is it a good idea to use getters and setters to access component state? (ReactTypescript)
Videos
There is only the (one) 'setState()' method - not a method per-property (as you've suggested/questioned).
It is a composite in terms of it's parameter, in that you can specify/set more than one item within the same/one call (to the 'setState()' method), so you can set all 20 of your items in one go.
E.g.
this.setState({ "firstName" : firstNameVal, "lastName" : lastNameVal });
I was starting from where you said you started - from a 'class' based component.
If you are sticking with the switch to a 'function' based component, then it is slightly different, in summary:
import React, { useState } from 'react';
...
// The 'useState' hook (function) returns a getter (variable) & setter (function) for your state value - and takes the initial/default value for it/to set it to, e.g.
const [ firstName, setFirstName ] = useState('');
And you then use the getter var to read it & the setter function to set it:
setFirstName('Dennis');
(I could be wrong but I believe 'hooks' were added in v16.8 of React.)
A more in-depth description:
https://www.simplilearn.com/tutorials/reactjs-tutorial/reactjs-state#:~:text=1%20A%20state%20can%20be%20modified%20based%20on,merge%20between%20the%20new%20and%20the%20previous%20state
Use useState hook in functional components.
const App = () => {
const [state, setState] = React.useState({ first: "hello", second: "world" });
return (
<div>
<input type="text" value={state.first} onChange={(ev) => setState({...state, first: ev.target.value})} />
<input type="text" value={state.second} onChange={(ev) => setState({...state, second: ev.target.value})} />
</div>
)
}
ReactDOM.render(<App />, document.getElementById('app'))
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<div id="app"> </div>
states should be like this
const [count,setCount] = React.useState(0); //setState
function count_checkbox() {
var applicable = [];
const cbs = document.querySelectorAll('input[class*="checkbox_value"]');
cbs.forEach((cb) => {
if (cb.checked) {
applicable.push(parseInt(cb.value));
}
});
setCount(applicable.length) //Update State here
}
setCount sets the state count.
Updating the state in a functional component is different from a class component. The useState hook returns an array consisting of the value and a setter.
You then call the setter function to update the value of the state.
const [count, setCount] = React.useState(0);
function count_checkbox() {
...
setCount(applicable.length)
}