@ibragim_2008

Почему выдаёт ошибку 'char' is not defined?

Ошибка: Line 19:13: 'char' is not defined no-undef.

Код:
import React, {Component} from 'react';
import GotService from '../../services/gotService'
import './charDetails.css';
export default class CharDetails extends Component {

    gotService= new GotService()

    state = {
        char: null
    }

    componentDidMount() {
        this.updateChar();
    }

    updateChar() {
        const {charId} = this.props;

        if(!char) {
            return;
        }

        this.gotService.getCharacter(charId)
            .then((char) => {
                this.setState({ 
                  char
                })
            })
        }

    render()  {

        if(!this.state.char) {
            return <span className='select-error'>Please select a char</span>
        }

        const {name, gender, born, died, culture} = this.state.char;

        return (
            <div className="char-details rounded">
                <h4>{name}</h4>
                <ul className="list-group list-group-flush">
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Gender</span>
                        <span>{gender}</span>
                    </li>
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Born</span>
                        <span>{born}</span>
                    </li>
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Died</span>
                        <span>{died}</span>
                    </li>
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Culture</span>
                        <span>{culture}</span>
                    </li>
                </ul>
            </div>
        );
    }
}
  • Вопрос задан
  • 118 просмотров
Решения вопроса 1
Sergomen
@Sergomen
Просто делай добро и оно вернётся
у тебя нету переменной char. единственный charу тебя в объекте state.

Если ты хочешь использовать переменную char из этого объекта то можно сделать так:
import React, {Component} from 'react';
import GotService from '../../services/gotService'
import './charDetails.css';
export default class CharDetails extends Component {

    gotService= new GotService()

    state = {
        char: null
    }

    componentDidMount() {
        this.updateChar();
    }

    updateChar() {
        const {charId} = this.props;

        // if(!char) {
        if(!this.state.char) {
            return;
        }

        this.gotService.getCharacter(charId)
             .then((char) => {
                this.setState({ 
                  char
                })
            })
        }

    render()  {

        if(!this.state.char) {
            return <span className='select-error'>Please select a char</span>
        }

        const {name, gender, born, died, culture} = this.state.char;

        return (
            <div className="char-details rounded">
                <h4>{name}</h4>
                <ul className="list-group list-group-flush">
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Gender</span>
                        <span>{gender}</span>
                    </li>
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Born</span>
                        <span>{born}</span>
                    </li>
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Died</span>
                        <span>{died}</span>
                    </li>
                    <li className="list-group-item d-flex justify-content-between">
                        <span className="term">Culture</span>
                        <span>{culture}</span>
                    </li>
                </ul>
            </div>
        );
    }
}
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
sk8beemo
@sk8beemo
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы