React functional component using state

后端 未结 4 1871
生来不讨喜
生来不讨喜 2020-12-06 11:30

I\'m trying to implement a React smart component using functions as shown here https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlook

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 12:29

    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 
    }
    
    const App = (props) =>{
    
      const [value, setValue] = useState(0);
    
      const handleAddValue = () => {
        const newValue = value+1;
        setValue(newValue);
      }
    
      return (
        
    The Value is: {value}
    ); }

    If you want to read more about this new functionality, follow the following link.

    https://reactjs.org/docs/hooks-intro.html

提交回复
热议问题