简体   繁体   English

如何从另一个组件设置/获取一个组件的值?

[英]How do I set/get values of one component from another?

Background:背景:

I have a component (component A) that includes another component (MyColorPicker) within it.我有一个组件(组件 A),其中包含另一个组件(MyColorPicker)。 I am trying trying to set/get the value of MyColorPicker from component A.我正在尝试从组件 A 设置/获取 MyColorPicker 的值。

Problem:问题:

I am able to set the background color by passing a prop to MyColorPicker like so: <MyColorPicker background="#ff0000" /> .我可以通过将道具传递给MyColorPicker来设置背景颜色,如下所示: <MyColorPicker background="#ff0000" />

However, I want to be able to update the background dynamically (ie depending on user input) rather than harcoding it into the prop.但是,我希望能够动态更新背景(即取决于用户输入),而不是将其编码到道具中。 I also want to be able to get the current background color value in the handleSubmit function in Component A.我还希望能够在组件 A 中的handleSubmit function 中获取当前背景颜色值。

Component A Code:组件 A 代码:

// imports...

function myFunctionA(props) {

  const handleSubmit = useCallback(() => {
    console.log(textOrSelectFields);
  });

  return (
    <>
      <Form onSubmit={handleSubmit}>
        <MyColorPicker />
      </Form>
    </>
  )
}

MyColorPicker Code: MyColorPicker 代码:

// imports...

class MyBlockPicker extends React.Component {
  state = {
    background: this.props.background,
  };

  handleChangeComplete = (color) => {
    console.log(color);
    this.setState({ background: color.hex });
  };

  render() {
    return (
      <BlockPicker
        color={this.state.background}
        onChangeComplete={this.handleChangeComplete}
        triangle="hide"
      />
    );
  }
}

export default MyBlockPicker;

Any ideas on how to proceed here?关于如何在这里进行的任何想法? Thanks!谢谢!

You can pass the setState function to the child.您可以将setState function 传递给孩子。 If you are using class components you can wrap the setState so the child only has access to a limited amount of the state.如果您使用的是 class 组件,您可以包装setState ,这样孩子只能访问有限数量的 state。

ie IE

In myFunctionA在我的函数A

function myFunctionA(props) {

  const [selectedColor, setSelectedColor] = useState(null)

  const handleSubmit = useCallback(() => {
    console.log(textOrSelectFields);
  });

  return (
    <>
      <Form onSubmit={handleSubmit}>
        <MyColorPicker setSelectedColor/>
      </Form>
    </>
  )
}

In MyColorPicker在我的颜色选择器中

class MyBlockPicker extends React.Component {
  state = {
    background: this.props.background,
  };

  handleChangeComplete = (color) => {
    console.log(color);
    this.setState({ background: color.hex });
    this.props.setSelectedColor(color.hex)
  };

  render() {
    return (
      <BlockPicker
        color={this.state.background}
        onChangeComplete={this.handleChangeComplete}
        triangle="hide"
      />
    );
  }
}

export default MyBlockPicker;

BTW you are mixing class components and functional components which is going to be a pain as the code base grows...顺便说一句,您正在混合 class 组件和功能组件,随着代码库的增长,这将是一件痛苦的事情......

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

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