首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >道具在React初始化时未定义。为什么?

道具在React初始化时未定义。为什么?
EN

Stack Overflow用户
提问于 2018-12-22 01:36:33
回答 1查看 439关注 0票数 0

我正在使用React 360创建一个VR应用程序。我想要做的是最终创建一个应用程序,如这里所示的Facebook VR multi surface example。我正在使用该示例的代码,并尝试将其适合我的应用程序。但是,我在React组件的以下初始化过程中遇到了问题。

我有一个带有以下代码index.js的react-360应用程序

代码语言:javascript
复制
import React from 'react';
import connect from './Store';

import {
  asset,
  AppRegistry,
  StyleSheet,
  Text,
  View,
  VrButton,
} from 'react-360';

import Entity from 'Entity';


const ModelView = props => {

  *********** Problem **************
  Why are my props undefined if I am declaring it in the wrapper?
  **********************************

  return (
    <Entity
      style={{transform: [{scaleX: 1.25}, {scaleY: 1.25}]}}
      source={{obj: asset(`${props.planetName}.obj`), mtl: asset(`${props.planetName}.mtl`)}}
    />
  );
};

const ConnectedModelView = connect(ModelView);
export default ConnectedModelView;

AppRegistry.registerComponent('ModelView', () => ModelView);

从上面的代码中,我对ModelView的支持不应该是未定义的。在初始化时,它的值应该是earth

这些属性应该在Store.js中初始化。代码如下:

代码语言:javascript
复制
import React from 'react';

const State = {
  planetName: 'earth'
};

const listeners = new Set();

export default function connect(Component) {
  return class Wrapper extends React.Component {
    state = {
      planetName: State.planetName
    };

    _listener = () => {
      this.setState({
        planetName: State.planetName
      });
    };

    componentDidMount() {
      listeners.add(this._listener);
    }

    componentWillUnmount() {
      listeners.delete(this._listener);
    }

    render() {
      return (
        <Component
          {...this.props}
          planetName={this.state.planetName}
        />
      );
    }
  };
}

从facebook代码中获取一个页面,我正在做的是通过Store.connect方法初始化模型视图。这个方法在我通过State.planetName设置道具的ModelView周围创建了一个包装器。然而,我一直没有定义,我不知道为什么。我已经将代码的每一部分都硬编码为State.planetName的地球值,但它仍然是未定义的。道具没有被设置为我想要的值,我不确定为什么。有没有人能帮我解释一下为什么会这样?如果能帮上忙我会很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-22 01:42:13

看起来您呈现的是ModelView而不是ConnectedModelView

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53888800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档