A
A
Alex Ozerov2020-07-15 11:19:09
React
Alex Ozerov, 2020-07-15 11:19:09

Component not mounted?

Error text: "Warning: Can't call setState on a component that is not yet mounted...". I can't figure out why it's giving an error. I googled what needs to be launched through componentDidMount but it didn’t work either, or rather it didn’t change anything. What's the catch?

class Rate extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            date: '',
            currencyRat: {},
        }
        this.currency = ['USD', 'RUB', 'CAD'];
        this.geRate();
    }
    getRate() {
        fetch('https://api.exchangeratesapi.io/latest')
            .then(data => {
                return data.json
            })
            .then(data => {
                console.log(data)
                this.setState({ date: data.date })
            })
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2020-07-15
@ozerovlife

class Rate extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            date: '',
            currencyRat: {},
        }
        this.currency = ['USD', 'RUB', 'CAD'];
    }
    componentDidMount() {
        this.getRate(); // не в конструкторе надо вызывать, а в componentDidMount
    }
    getRate() {
        fetch('https://api.exchangeratesapi.io/latest')
            .then(data => {
                return data.json(); // забыли вызвать функцию
            })
            .then(data => {
                console.log(data)
                this.setState({ date: data.date })
            })
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question