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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…