react使用

    xiaoxiao2023-11-18  147

    react导入的包一定要有顺序:

    <script src="js/react.development.js"></script> <script src="js/react-dom.development.js"></script> <script src="js/babel.min.js"></script>

    jsx中的注释是{/*   注释内容 */}    先写一个大括号在ctrl+shift+/  可以快速生成.

    {}里面是变量的写入.将sTr进行字符串前后进行颠倒代码如下:

    <p>{ sTr.split('').reverse().join('') }</p>

    属性(props) 

    状态(state)  更新组件是setState,而不是state,  state的值可能是异步的,如果需要在state的值上修改得到新的值,可以使用函数的形式,函数的参数中传递的第一个参数是state上一个状态的值.

    列表渲染?

    表单数据绑定?

    生命周期方法.     两种方法:componentDidMount和componentWillUnmount

                              // 组件初始化时自动执行的方法 

    componentDidMount() { console.log('componentDidMount'); }

                              //组件销毁时自动执行的方法

    componentWillUnmount(){ console.log('componentWillUnmount'); }

    数据交互   axios

    常用参数: 1. url请求地址          

                     2.method请求方式.默认是`GET`,常用的还有`POST`

                     3.responsetype设置返回的数据格式,常用的是`json`格式,也可以设置为`text`或者`html`

                     4.params(参数)设置发送给服务器的数据

                     5.then设置请求成功后的回调函数.

                     6.catch设置请求失败后的回调函数.

    axios完整写法:

    axios({ url: '/user/12345', method: 'get', responsetype:'json', params: { firstName: 'Fred', lastName: 'Flintstone' } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
    最新回复(0)