【问题标题】:Home(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return nullHome(...):渲染没有返回任何内容。这通常意味着缺少 return 语句。或者,不渲染任何内容,返回 null
【发布时间】:2018-12-08 14:39:42
【问题描述】:

我想将所有 documents 存储在 firebase 中。如果有超过 0 个文档,则渲染 1,否则打印 no doc found 但这会给我错误:

render() {
    firebase
      .firestore()
      .collection("users")
      .doc(localStorage.getItem("ph"))
      .collection("chat")
      .get()
      .then(d => {
        this.dt = d.docs.length;
        if (this.dt > 0) {
          return <div>1</div>;
        } else {
          return (
            <div>
              <div className="app-noFoundArea">
                <img src={noFound} />
              </div>
              <div className="app-noFound-title">
                <p>
                  No chat found. Try create using{" "}
                  <button className="app-add-icon app-nofound">
                    <i className="material-icons">add</i>
                  </button>
                </p>
              </div>
            </div>
          );
        }
      })
      .catch(e => {});
  }

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    获取render 中的数据可能不是一个好主意,因为每次更新stateprops 时都会发生这种情况。它也是异步的,因此 React 实际上不会返回任何内容来渲染。

    你最好把firebase逻辑放在componentDidMount

    class MyComponent extends React.Component {
      state = { loaded: false, docs: [] };
    
      componentDidMount() {
        firebase
          .firestore()
          .collection("users")
          .doc(localStorage.getItem("ph"))
          .collection("chat")
          .get()
          .then(d => {
            this.setState({ loaded: true, docs: d });
          })
          .catch(e => {
            this.setState({ loaded: true });
          });
      }
    
      render() {
        const { loaded, docs } = this.state;
    
        if (!loaded) {
          return null;
        }
    
        if (docs.length > 0) {
          return <div>1</div>;
        } else {
          return (
            <div>
              <div className="app-noFoundArea">
                <img src={noFound} />
              </div>
              <div className="app-noFound-title">
                <p>
                  No chat found. Try create using{" "}
                  <button className="app-add-icon app-nofound">
                    <i className="material-icons">add</i>
                  </button>
                </p>
              </div>
            </div>
          );
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      您必须返回在 firebase 函数中做出的响应。

      render() {
      return (
      <div>
        {
          firebase.firestore().collection("users").doc(localStorage.getItem("ph")).collection("chat").get()
              .then((d) => {
                  this.dt = d.docs.length;
                  if (this.dt>0 ) {
                    return (<div>1</div>)
                  } else {
                    return (
                      <div>
                        <div className="app-noFoundArea">
                            <img src={noFound}></img>
                        </div>
                        <div className="app-noFound-title">
                            <p>No chat found. Try create using <button className="app-add-icon app-nofound"><i className="material-icons">add</i></button></p>
                        </div>
                      </div>
                  )
                }
            })
            .catch((e) => {
            })    
        }
      </div>
       )
      }
      

      【讨论】:

        【解决方案3】:

        您将 A 组件和 JS 代码混合在一起,如果它属于 catch 函数,则它不会返回任何内容。

        React.Component 中的 render 方法总是期望返回一个有效的 HTML 结构。

        您可以尝试在代码中进行以下修改

        constructor(props) {
            super(props)
            this.state = {
             IsChatFound : false
            };
          }
        
          getChatInfo = () => {
           try{
                firebase.firestore().collection("users").doc(localStorage.getItem("ph")).collection("chat").get()
                .then((d) => {
                    this.setState({IsChatFound :(d.docs && d.docs.length > 0)}); 
                });
            }
            catch(error){
              this.setState({IsChatFound : false}); 
            }
          }
        
          componentDidMount =()=>{
            this.getChatInfo();
          }
        
        
          render() {
            return (
                {this.state.IsChatFound ?
                    <div>1</div>
                : <div>
                    <div className="app-noFoundArea">
                        <img src={noFound}></img>
                    </div>
                    <div className="app-noFound-title">
                        <p>No chat found. Try create using <button className="app-add-icon app-nofound"><i className="material-icons">add</i></button></p>
                    </div>
                 </div>
                }
            )         
         }
        

        你现在看到 render() 方法总是返回 HTML

        【讨论】:

          猜你喜欢
          • 2021-05-13
          • 2019-08-14
          • 1970-01-01
          • 1970-01-01
          • 2018-03-26
          • 2020-07-06
          • 2022-01-12
          • 2021-05-15
          • 2021-03-05
          相关资源
          最近更新 更多