最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - error: attempted to update component that has already been unmounted (or failed to mount) - Stack Overflow

programmeradmin7浏览0评论

I'm new to React, I'm having trouble rendering my app due to this error. It's seems the data that I'm trying to render as elements won't render due to trying to set state when unmounted?

I'm not sure how I'm getting this error, as I'm setting the state of Data in ponentDidMount.

How can I fix this issue?

error: attempted to update ponent that has already been unmounted (or failed to mount)

class Profile extends React.PureComponent {
  static propTypes = {
    navigation: PropTypes.object,
    handleLogout: PropTypes.func,
    user: PropTypes.object,
  };

  static navigationOptions = {
    headerVisible: true,
    title: 'Profile',
  };

constructor(props) {
    super(props);

    this.state = {
    data: [],
    loading: true

    };
  }

ponentDidMount() {

  fetch(":8080/bined", { method: 'get' })
    .then(response => response.json())
    .then(data => {
      this.setState({data: data,});

    })
   .catch(function(err) {
     // 
   })
}

 render() {

    const { user } = this.props;
    let email;

    if (user) {
      email = user.rows[0].ACCTNO;
    }
    return (
      <ContentWrapper>
        <View style={styles.container}>
          <Image style={styles.header} source={images.profileHeader} />
          <View style={styles.body}>
            <Avatar email={email} style={styles.avatar} />
             {
   this.state.data.map(function(rows, i){
         this.setState({mounted:  true});

    return(
      <View key={i}>
        <Text>{rows.FIRSTNAME}</Text>
      </View>
    );
  })
}            <Text style={styles.email}>{_.capitalize(email)}</Text>

            <Button
              title="Log out"
              icon={{ name: 'logout-variant', type: 'material-munity' }}
              onPress={this.logout}
              style={styles.logoutButton}
            />
          </View>
        </View>
      </ContentWrapper>
    );
  }
}

export default Profile;

I'm new to React, I'm having trouble rendering my app due to this error. It's seems the data that I'm trying to render as elements won't render due to trying to set state when unmounted?

I'm not sure how I'm getting this error, as I'm setting the state of Data in ponentDidMount.

How can I fix this issue?

error: attempted to update ponent that has already been unmounted (or failed to mount)

class Profile extends React.PureComponent {
  static propTypes = {
    navigation: PropTypes.object,
    handleLogout: PropTypes.func,
    user: PropTypes.object,
  };

  static navigationOptions = {
    headerVisible: true,
    title: 'Profile',
  };

constructor(props) {
    super(props);

    this.state = {
    data: [],
    loading: true

    };
  }

ponentDidMount() {

  fetch("http://10.0.2.2:8080/bined", { method: 'get' })
    .then(response => response.json())
    .then(data => {
      this.setState({data: data,});

    })
   .catch(function(err) {
     // 
   })
}

 render() {

    const { user } = this.props;
    let email;

    if (user) {
      email = user.rows[0].ACCTNO;
    }
    return (
      <ContentWrapper>
        <View style={styles.container}>
          <Image style={styles.header} source={images.profileHeader} />
          <View style={styles.body}>
            <Avatar email={email} style={styles.avatar} />
             {
   this.state.data.map(function(rows, i){
         this.setState({mounted:  true});

    return(
      <View key={i}>
        <Text>{rows.FIRSTNAME}</Text>
      </View>
    );
  })
}            <Text style={styles.email}>{_.capitalize(email)}</Text>

            <Button
              title="Log out"
              icon={{ name: 'logout-variant', type: 'material-munity' }}
              onPress={this.logout}
              style={styles.logoutButton}
            />
          </View>
        </View>
      </ContentWrapper>
    );
  }
}

export default Profile;
Share Improve this question edited May 31, 2019 at 13:24 Felipe Augusto 8,18412 gold badges43 silver badges75 bronze badges asked May 17, 2017 at 17:23 sortinousnsortinousn 6472 gold badges9 silver badges28 bronze badges 1
  • 1 Your map function is why the error is occurring. You call setState inside render(). – Alex Harrison Commented May 17, 2017 at 18:01
Add a ment  | 

4 Answers 4

Reset to default 6

In your render function, you're calling this.setState({mounted:true}). From React's ponent documentation:

The render() function should be pure, meaning that it does not modify ponent state, it returns the same result each time it's invoked, and it does not directly interact with the browser.

Here's the link to the React documentation on the render function.

There is another way this error can occur...thinking that props are individual arguments (props is actually a single argument)

import React from 'react';

const Posts = posts => {       // WRONG

const Posts = ({posts}) => {   // RIGHT
  const renderPosts = () => {
    return posts.map(post => <div>{post.title}</div>);
  };

  return <div>{renderPosts()}</div>;
};

My problem is I forget

import React from 'react'

in my .jsx file, since I thought importing React is not needed in functional ponent.

My problem was in Sider, Then inside Link tag I did misspell instead of /location I wrote /locaion .

 <Menu.Item key="2">
      <Icon type="settings" />
      <span><Link style={styles.linkStyle} to="/locations">Locations</Link></span>
    </Menu.Item>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论