简体   繁体   English

使用React.Children从外部调用内部组件方法

[英]Calling an internal component method externally using React.Children

I've created a validation function that I can call externally like so: 我创建了一个验证函数,可以像这样从外部调用:

const isValid = validateChildren(this.props.children)

And I have a component I'd like to validate. 我有一个要验证的组件。

class MyComponent extends React.Component {
 constructor(props) {
  super(props)
 } 

 isValid() {
  // Validation will check against the render method in this component.
  return true;
 }

 render() {
  return false;
 }

}

Within that function I'm using the component props to check for a validation function using React.Children . 在该函数中,我使用组件props使用React.Children检查验证功能。 This looks something like this: 看起来像这样:

React.Children.map(children, (child) => {
 // Validation here.
});

What I'd like to do in addition to checking for props, is to check for a internal class method of isValid and then fire it. 除了检查道具外,我还想检查isValid的内部类方法,然后将其触发。 That way in the case of MyComponent I could do the following: MyComponent的情况下,我可以执行以下操作:

if (child.current.isValid) child.current.isValid()

Is something like this possible in React? 在React中有可能这样的事情吗? I'm trying to solve a performance issue with cloning the child elements that I'd like to avoid with this approach. 我正在尝试通过克隆希望通过这种方法避免使用的子元素来解决性能问题。

You can do this using forwardRef and the useImperativeHandle hook, as described here . 为此,您可以使用forwardRefuseImperativeHandle挂钩,如所描述这里

If you change the name in the App function, you'll see the validity change. 如果在App函数中更改名称,则会看到有效性更改。

import React, { useState, useImperativeHandle, useRef, useEffect } from "react";
import ReactDOM from "react-dom";

const validateNameProp = nameProp => {
  return nameProp === "Colin";
};

let Child = ({ name, childRef }) => {
  const [nameIsValid, setNameIsValid] = useState(false);

  // We want to expose the isValid function so it can be called by parent.
  useImperativeHandle(childRef, () => ({
    isValid
  }));

  const isValid = () => {
    setNameIsValid(true);
  };

  return (
    <div ref={childRef}>
      <h1>
        Name is {name} and this name is: {nameIsValid ? "valid" : "invalid"}
      </h1>
    </div>
  );
};

const App = () => {
  const childRef = useRef();
  const name = "Colin";

  // Wait until component mounts so ref is not null.
  useEffect(() => {
    if (validateNameProp(name)) {
      childRef.current.isValid();
    }
  }, []);

  return <Child childRef={childRef} name={name} />;
};

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

编辑jz80w2q76w

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM