代码之家  ›  专栏  ›  技术社区  ›  enbermudas

返回json对象到呈现方法-react.js

  •  2
  • enbermudas  · 技术社区  · 8 年前

    我正在消耗 pokeapi 为了一个私人项目,想在做的时候学会反应。一旦成功地从api中检索到所需的信息,如下所示:

    handleSubmit(event) {
    
            axios.get('https://pokeapi.co/api/v2/pokemon/' + this.state.input_id).then(r => {
    
                var data = r.data;
                var types = [];
                for (let i = 0; i < data.types.length; i++) types.push(data.types[i].name);
    
                const pokemon = {
                    id: data.id,
                    name: data.name,
                    sprite: data.sprites.front_default,
                    height: data.height,
                    weight: data.weight,
                    types: types,
                };
    
                this.setState({
                    pokemon: pokemon
                });
    
            }).catch(e => {
    
                console.log(e);
    
            }); // axios
    
            event.preventDefault();
    
        } // handleSubmit
    

    我只想渲染这个神奇宝贝。我现在就是这样做的:

    render() {
            return (
                <div>
                    <form onSubmit={this.handleSubmit}>
                        <label>
                            Pokemon's id
                            <input type="text" value={this.state.input_id} onChange={this.handleChange} />
                        </label>
                        <input type="submit" value="Find!"/>
                    </form>
                    <code>{this.state.pokemon}</code>
                </div>
            );
        } // render
    

    当然,这会引发下一个错误:

    Objects are not valid as a React child (found: object with keys {id, name, sprite, height, weight, types}). If you meant to render a collection of children, use an array instead.
    

    是否有方法在表中呈现此对象?我想这是没有保存州内的数据,但我不知道如何这样做。

    3 回复  |  直到 8 年前
        1
  •  2
  •   Rohit Nethi    8 年前

    不能在react中直接打印对象或数组。同时 {JSON.stringify(this.state.pokemon)} 当然可以工作并允许您将对象打印为字符串,如果需要,还可以单独打印对象属性。

    您可以通过更改 {this.state.pokemon} 有点像-

    {this.state.pokemon.id}
    {this.state.pokemon.name} 
    {this.state.pokemon.sprite}
    {this.state.pokemon.height}
    {this.state.pokemon.weight}
    {this.state.pokemon.types}
    
        2
  •  2
  •   Tholle    8 年前

    react不允许您将对象作为react子对象,因此您可以使用 JSON.stringify 第一:

    <code>{JSON.stringify(this.state.pokemon)}</code>
    
        3
  •  1
  •   Manoj    8 年前

    Althought React不允许渲染对象。您可以创建一个与render并行的函数,并将对象传递给它,然后在该函数中进行渲染;如下所示。

    _render(obj){
       return <code>{obj}</code> //here also jsx syntax are valid.
    }
    
    render() {
            return (
                <div>
                    <form onSubmit={this.handleSubmit}>
                        <label>
                            Pokemon's id
                            <input type="text" value={this.state.input_id} onChange={this.handleChange} />
                        </label>
                        <input type="submit" value="Find!"/>
                    </form>
                    {this._render(this.state.pokemon)}
                </div>
            );
        } // render