美文网首页
react学习(15)函数式组件中使用props

react学习(15)函数式组件中使用props

作者: 哆啦C梦的百宝箱 | 来源:发表于2022-08-17 10:13 被阅读0次

    对于state,props和refs,抛开hooks不说,也只有props能在函数式组件中使用,因为函数式组件中的this为undefined.

            <script type="text/babel">
            function Person(props){
                return (
                    <ul>
                        <li>姓名:{props.name}</li>
                        <li>年龄:{props.age}</li>
                        <li>性别:{props.sex}</li>
                    </ul>
                )
            }
            Person.propTypes ={
                name:PropTypes.string.isRequired,
                age:PropTypes.number,
                sex:PropTypes.string
            }
            Person.defaultProps = {
                age:18,
                sex:'男'
            }
            ReactDOM.render(<Person name="tom" age={18} sex="女"/>,document.getElementById('test'));
        
        </script>
    
    

    相关文章

      网友评论

          本文标题:react学习(15)函数式组件中使用props

          本文链接:https://www.haomeiwen.com/subject/ghorgrtx.html