Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 浅层渲染组件和scryRenderedComponentsWithType_Javascript_Reactjs - Fatal编程技术网

Javascript 浅层渲染组件和scryRenderedComponentsWithType

Javascript 浅层渲染组件和scryRenderedComponentsWithType,javascript,reactjs,Javascript,Reactjs,我一直在玩reactjsTestUtils。这让人很沮丧。我不确定,但经过几个小时的实验。在我看来,在对组件进行浅层渲染之后,我无法使用findRenderedComponentWithType或scryRenderedComponentsWithType。事实上,我不知道如何从组件树中按类型提取对象。我不知道为什么,因为我对reactjs还很陌生,我也不知道我能做什么或不能做什么 例如: var Layout = React.createClass({ render: function

我一直在玩
reactjs
TestUtils
。这让人很沮丧。我不确定,但经过几个小时的实验。在我看来,在对组件进行浅层渲染之后,我无法使用
findRenderedComponentWithType
scryRenderedComponentsWithType
。事实上,我不知道如何从
组件树
中按类型提取对象。我不知道为什么,因为我对
reactjs
还很陌生,我也不知道我能做什么或不能做什么

例如:

var Layout = React.createClass({
    render: function(){
        console.log(this.props);
        return (
            <div id="data-trader">
                <header className="header">
                    <LogoElement />
                    <UserMenu user={this.props.user} />
                </header>
                <div className="container max">
                    <div className="main">
                        <RouteHandler path={ this.props.path } query={ this.props.query } params={ this.props.params } user={this.props.user} />
                    </div>
                    <Footer />
                </div>
            </div>
        );
    }
});

我也遇到了同样的问题,在查看代码之后,我发现问题是scry/find调用findAllInRenderedTree并测试DOM元素:

假设两个测试都返回false,那么该方法将返回一个空数组。(这会导致find中描述的错误,并导致scry返回空数组)

我认为这是因为shallwrender的输出不是DOM元素(这就是为什么shallwrender很棒:)

如果你想找到某种类型的浅树的第一个子树,你可以这样做:

  findChildrenOfType (view, Component) {
    if (!view) return null;

    if (reactTestUtils.isElementOfType(view, Component)) return view;

    if (view.length) { // is an Array
      for (let i = 0; i < view.length; i++) {
        let found = this.findChildrenOfType(view[i], Component);
        if (found) return found;
      }
    } else {
      if (!view.props || !view.props.children) return null;
      let children = view.props.children;

      return this.findChildrenOfType(children, Component);
    }
  }
findChildrenOfType(视图、组件){
如果(!view)返回null;
if(reactTestUtils.isElementOfType(视图,组件))返回视图;
如果(view.length){//是数组
for(设i=0;i
希望这有帮助

Error: Did not find exactly one match for componentType:function UserMenu()
  findChildrenOfType (view, Component) {
    if (!view) return null;

    if (reactTestUtils.isElementOfType(view, Component)) return view;

    if (view.length) { // is an Array
      for (let i = 0; i < view.length; i++) {
        let found = this.findChildrenOfType(view[i], Component);
        if (found) return found;
      }
    } else {
      if (!view.props || !view.props.children) return null;
      let children = view.props.children;

      return this.findChildrenOfType(children, Component);
    }
  }