Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app.tsx render内无法通过条件判断渲染this.props.children #11396

Closed
talentjeff opened this issue Mar 4, 2022 · 4 comments
Closed

app.tsx render内无法通过条件判断渲染this.props.children #11396

talentjeff opened this issue Mar 4, 2022 · 4 comments
Labels
F-react Framework - React resolved T-h5 Target - 编译到 H5 V-3 Version - 3.x

Comments

@talentjeff
Copy link

talentjeff commented Mar 4, 2022

相关平台

H5

浏览器版本: Chrome 98.0.4758.102
使用框架: React

复现步骤

修改app.tsx

  constructor(props) {
    super(props);
    this.state = { userInfoSeted: false };
  }

  componentWillMount() {
    setTimeout(() => {
      this.setState({ userInfoSeted: true })
    }, 1000);
  }

  render() {
    return <Provider store={store}>
      {
        this.state.userInfoSeted ? this.props.children : <Text>Loading</Text>
      }
    </Provider>;
  }

期望结果

期望在app渲染前,先加载一些数据

实际结果

页面报错,提示“没有找到页面实例。”

环境信息

👽 Taro v3.3.19

undefined

  Taro CLI 3.3.19 environment info:
    System:
      OS: Windows 10
    Binaries:
      Node: 14.17.5 - C:\Program Files\nodejs\node.EXE
      Yarn: 1.22.11 - ~\AppData\Roaming\npm\yarn.CMD
      npm: 6.14.14 - C:\Program Files\nodejs\npm.CMD

补充信息

可以理解成有些数据必须在app执行页面逻辑前获取。发现通过控制app里的render会报错。
请问应该如何解决,谢谢!
Taro版本3.3.19

@taro-bot2 taro-bot2 bot added F-react Framework - React T-h5 Target - 编译到 H5 V-3 Version - 3.x labels Mar 4, 2022
@ZakaryCode ZakaryCode added the enhancement New feature or request label Mar 7, 2022
@ZakaryCode
Copy link
Contributor

不建议这么使用,因为在渲染小程序时,不可以在页面外渲染元素,同时渲染 h5 时也不应对页面作出修改,否则会导致一些额外的问题。尽管如此这个需求依旧有解法来实现,可以通过如下的方法来避开这个错误:

import { Text } from '@tarojs/components'
import { Component } from 'react'

import './app.scss'

class App extends Component {
  state = { userInfoSettled: false }

  componentWillMount() {
    setTimeout(() => {
      this.setState({ userInfoSettled: true })
    }, 1000);
  }

  render() {
    return <>
      <div style={{ display: this.state.userInfoSettled ? 'block' : 'none' }}>{this.props.children}</div>
      {!this.state.userInfoSettled && <Text>Loading</Text>}
    </>
  }
}

export default App

@yoyo837
Copy link
Contributor

yoyo837 commented Sep 16, 2022

Vue3 版本呢? 如何解决这个问题.

@helloint
Copy link

helloint commented Nov 1, 2022

@ZakaryCode 那如果我需要异步加载config.json,然后把相关的data倒入redux,该如何处理呢?这就不能简单的display/hide了

import { Suspense, useEffect, useState } from 'react';
import { Provider } from 'react-redux';
import Taro from '@tarojs/taro';
import createStore from './store/store';
import './i18n';
import { Config, CONFIG_URL, httpClient } from './pages/utils';

import './app.scss';

const App = (props) => {
  const [config, setConfig] = useState<Config>();
  useEffect(() => {
    if (process.env.TARO_ENV === 'weapp') {
      Taro.cloud.init();
    }

    httpClient(CONFIG_URL).then((data: Config) => {
      setConfig(data);
    });
  }, []);

  return config ? (
    <Provider store={createStore({ config: { locale: config.locale } })}>
      <Suspense fallback='loading...'>{props.children}</Suspense>
    </Provider>
  ) : (
    ''
  );
};
export default App;

请指点,thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React resolved T-h5 Target - 编译到 H5 V-3 Version - 3.x
Projects
Archived in project
Development

No branches or pull requests

4 participants