Ver Fonte

fix loginInput setState warning

Csy817 há 7 anos atrás
pai
commit
b92d625b4d
2 ficheiros alterados com 20 adições e 10 exclusões
  1. 2 0
      src/app/basicVersion/exampleShow/ExampleShow.jsx
  2. 18 10
      src/login/LoginInput.js

+ 2 - 0
src/app/basicVersion/exampleShow/ExampleShow.jsx

@@ -20,6 +20,8 @@ class ExampleShow extends Component {
 
     componentWillMount() {
         request(graphqlUrl, SHOW_CASE, {user_id: 'ioobot'}).then(data => {
+            console.log('eample data',data.case_by_props);
+            // Object.prototype.toString.call(data.case_by_props) === '[object Array]';
                 if (Object.prototype.toString.call(data.case_by_props).split(' ')[1].slice(0, -1).toLowerCase() === 'array') {
                     this.setState({
                         examples: data.case_by_props

+ 18 - 10
src/login/LoginInput.js

@@ -37,12 +37,12 @@ class LoginInput extends Component {
         }
     }
 
-
     login = () => {
         let _this = this;
+        this._isMounted = true;
         // let loginUrl = `http://localhost:8999/login`;
 
-        if (this.state.login_username !== '' && this.state.login_password !== '') {
+        if (this.state.login_username !== '' && this.state.login_password !== '' ) {
             axios.post(loginUrl, {
                 'user-name': this.state.login_username,
                 password: this.state.login_password
@@ -56,10 +56,12 @@ class LoginInput extends Component {
                     setCookie("user_id", res.data);
                     _this.props.getUserId(res.data);
                     request(graphqlUrl, GET_USER, {id: res.data}).then(data => {
-                            this.setState({
-                                avatar: data.user_by_id.avatar,
-                                nickname: data.user_by_id.nickname
-                            })
+                            if (this._isMounted) {
+                                this.setState({
+                                    avatar: data.user_by_id.avatar,
+                                    nickname: data.user_by_id.nickname
+                                })
+                            }
                         }
                     )
                 })
@@ -181,6 +183,7 @@ class LoginInput extends Component {
 
     componentWillMount() {
         let _this = this;
+        this._isMounted = true;
         axios.get(getIdUrl)
             .then((res) => {
                 if (res.data !== '') {
@@ -191,15 +194,16 @@ class LoginInput extends Component {
                             loginOnce: false,
                             loginStatus: 'logined'
                         }, () => {
-                            request(graphqlUrl, GET_USER, {id: res.data}).then(data => {
+                        request(graphqlUrl, GET_USER, {id: res.data}).then(data => {
+                                if (this._isMounted) {
                                     this.setState({
                                         avatar: data.user_by_id.avatar,
                                         nickname: data.user_by_id.nickname
                                     })
                                 }
-                            )
-                        }
-                    )
+                            }
+                        )
+                    });
                 }
             })
             .catch((err) => {
@@ -207,6 +211,10 @@ class LoginInput extends Component {
             });
     }
 
+    componentWillUnmount() {
+        this._isMounted = false
+    }
+
     render() {
         return (
             <div>