简体   繁体   English

在props中将函数传递给React组件

[英]Passing a function to React component in props

I checked other questions and also docs but I cannot make it work. 我检查了其他问题文档,但无法使其正常工作。 I want to pass a handler function to a child component within its props to change between themes. 我想将处理函数传递给其道具内的子组件,以在主题之间进行更改。

I defined a state in the parent component and the handler that sets the new state. 我在父组件和设置新状态的处理程序中定义了一个状态。 I tried with and without a parameter. 我尝试使用和不使用参数。

This is the parent 这是父母

class MyApp extends App {

  constructor(props) {
    super(props);
    this.state = {
      colorScheme: "light"
    };
    this.handleColorScheme = this.handleColorScheme.bind(this);
  }

  handleColorScheme = scheme => {
    this.setState({ colorScheme: scheme });
  };

  render() {
    const { Component, pageProps } = this.props;

    const props = {
      ...pageProps,
      schemeHandler: this.handleColorScheme
    };
    return (
      <Container>
         <ThemeProvider
            theme={this.state.colorScheme === "light" ? theme : themeDark}
          >
            <Component {...props} />
          </ThemeProvider>
      </Container>
    );
  }
}

export default MyApp;

And this the child component 这是子组件

export default function Layout(props) {
  const [theme, setTheme] = useState({
    palette: {
      type: "light"
    }
  });

  const toggleDarkTheme = () => {
    let newPaletteType = theme.palette.type === "light" ? "dark" : "light";    
    props.schemeHandler(newPaletteType);
  };

  return (
   <div>
      <CssBaseline />
      <div className={classes.root}>
        <AppBar
          position="absolute"
          color={"default"}
          className={classes.appBar}
        >
          <Toolbar>
              <IconButton onClick={toggleDarkTheme}>
                <Dark />
              </IconButton>            
          </Toolbar>
        </AppBar>
        <main className={classes.content}>          
          {props.children}
        </main>
      </div>
    </div>
  );
}

When I click the button it says that toggleDarkTheme is not a function. 当我单击按钮时,它说toggleDarkTheme不是函数。 Debugging with dev tools I see that I never reach parent function. 使用开发工具进行调试时,我看不到父函数。 How can this function be passed in props? 该函数如何在prop中传递?

  1. You should not use state in both parent and children, it makes your logic become complicated. 您不应在父母和子女中都使用状态,这会使您的逻辑变得复杂。 You can place the state in parent and pass the modifier function as prop to children (children become dumb component). 您可以将状态放置在父级中,并将修饰符函数作为prop传递给子级(子级变为哑组件)。

  2. No need to bind the modifier function. 无需绑定修饰符函数。

  3. Parent component can become a function instead of a class. 父组件可以成为一个函数而不是一个类。

Example: https://codesandbox.io/embed/passing-react-func-as-props-kf8lr 示例: https//codesandbox.io/embed/passing-react-func-as-props-kf8lr

I suspect it is because of syntax. 我怀疑是因为语法。

<IconButton onClick={() => toggleDarkTheme() }>

Changing this line in your child component should do the trick. 在子组件中更改此行应该可以解决问题。 Cant really know without having some minimal code to test run Cant确实知道,而没有一些最少的代码来测试运行

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

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