繁体   English   中英

出现此错误的学校项目:渲染未返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null

[英]School project with this error: Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null

我正在为学校开展一个项目,但我的代码的非渲染部分出现此错误。

fetchCharacterData = () => {
    var encodedURI = window.encodeURI(this.props.uri);
    return axios.get(encodedURI).then(response => {
        this.setState(() => {
            return {
                characterData: response.data
            };
        });
    });
};

读取this.setState(() => {我尝试了其他问题的建议,但似乎没有任何内容适用于这行代码。

componentDidMount() {
    this.fetchCharacterData();
    console.log("MOUNT");
}

render() {
    console.log(this.state.characterData);
    if (this.state.characterData.length === 0) {
       return <div>Failed to fetch data from server</div>;
    }
    const characters = this.state.characterData.map(character => {
       return <div key={character.name}><em>: {character.name}</em>: {character.class} </div>
    });
}

您需要在渲染函数中返回一些内容 - 该错误与您粘贴的代码无关。

class YourComponent extends React.Component {

    // ...

    fetchCharacterData = () => {
        var encodedURI = window.encodeURI(this.props.uri);
        return axios.get(encodedURI).then(response => {
            this.setState(() => {
                return {
                    characterData: response.data
                };
            });
        });
    };

    // ...

    // It's this part causing the error.
    // It either returns nothing, or doesn't exist.
    render() {
        return <div>Your page content</div> // or return null
    }
}

尝试像这样调整渲染功能。

render() {
    if (this.state.characterData.length === 0) {
       return <div>Failed to fetch data from server</div>;
    }
    const characters = this.state.characterData.map(character => {
       return <div key={character.name}><em>: {character.name}</em>: {character.class} </div>
    });
    /******************/
    return characters;
    /******************/
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null Home(…):渲染未返回任何内容。 这通常意味着缺少return语句。 或者,不渲染任何内容,则返回null unboundStoryFn(…):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null 我收到此错误 &gt;&gt; 渲染未返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null 反应错误:渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null 错误:StateProvider(...):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null 错误:聊天(...):渲染没有返回任何内容。 这通常意味着缺少 return 语句。 或者,不渲染任何内容,返回 null Index.js 错误:UserForm(…):没有从渲染返回。 这通常意味着缺少 return 语句。 或者,什么也不渲染,返回 null React - 错误:应用程序(...):渲染没有返回任何内容。 这通常意味着缺少返回语句。 或者,不渲染任何内容,返回 null 错误:RenderComments(...) 渲染没有返回任何内容。 这通常意味着缺少返回语句。 或者,不渲染任何内容,返回 null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM