简体   繁体   English

如何使用 onClick 函数在子组件中设置父组件的状态?

[英]How do I set the state of a parent component in a child component using an onClick function?

I will only show code that is relevant.我只会显示相关的代码。 This is the code from the parent component :这是parent component的代码:

export default class App extends React.Component {

constructor(props) {
    super(props);
    this.state = {
        activeScreen: 'dashboard',
        authenticatedUser: this.getAuthenticatedUser(),
    }
}

setActiveScreen(key) {
    this.setState({
        activeScreen: key
    })
}

style = (label) => {
    if (label === 'Dashboard') {
        return {'border-top': '5px solid red'}
    }
}

render() {
    return (
        <div>
            {/* {this.serviceWorker()} */}
            <ToastContainer transition={Slide} />
            <Router>
                <Switch>
                    <NavBar  
authenticatedUser={this.state.authenticatedUser} activeScreen={this.state.activeScreen} 
setActiveScreen={this.setActiveScreen()} />

                </Switch>
            </Router>
        </div>
    );
}

} }

The code from the child component Navbar is:子组件Navbar的代码是:

class NavBar extends Component {

constructor(props) {
    super(props);
}

adminMenu() {
    return (
        <div className='admin-menu'>
            {this.adminNav().map((i, key) => (
                        <li className="nav-item" style={this.props.style(i.label)} key={key}>
                            <Link className={`nav-link ${this.props.activeScreen == i.key ? 'active' : ''}`} to={i.to} onClick={() => this.props.setActiveScreen(i.key)}>
                                <i className={`fa fa-fw fa-${i.icon}`} aria-hidden="true"></i>
                                {i.label}
                                {this.props.activeScreen == i.key &&
                                    <span className="sr-only">(current)</span>
                                }
                            </Link>
                        </li>
            ))}
        </div>
    )
}

render() {
    return (
        {this.adminMenu()}
    );
}

adminNav() {
    return [
        {
            label: 'Dashboard',
            icon: 'tachometer',
            key: 'dashboard',
            to: '/',
            active: true,
            admin: false,
            sales: false
        },
        there are more of these objects
    ]
}
}

export default NavBar;

The error I get is Cannot update during an existing state transition (such as within render or another component's constructor).我得到的错误是无法在现有状态转换期间更新(例如在render或其他组件的构造函数中)。 Render methods should be a pure function of props and state; Render 方法应该是 props 和 state 的纯函数; constructor side-effects are an anti-pattern, but can be moved to componentWillMount .构造函数的副作用是一种反模式,但可以移动到componentWillMount The stack trace says it is in setActiveScreen .堆栈跟踪说它在setActiveScreen I am unable to fix this problem.我无法解决这个问题。

My syntax is all fine but may not seem so as I have deleted lots of unrelated code.我的语法都很好,但可能看起来不像,因为我删除了很多不相关的代码。

You are invoking the function with no parameters.您正在调用没有参数的函数。 Update your code with this;用这个更新你的代码;

setActiveScreen={this.setActiveScreen}

OR或者

setActiveScreen={(key) => this.setActiveScreen(key)}

暂无
暂无

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

相关问题 React - 如何使用子组件设置我的父 state,使用 function 结构(不是类) - React - How can I set my parent state using child component, using function structures (not classes) 使用react-router时,如何从子组件中设置父组件的状态? - How can I set the state of my parent component from the child component when using react-router? 如何使用标记的模板文字在子样式组件中获取父组件的 state? - How do I get the state of the parent component in a child styled component using tagged template literals? 如何从一个子组件在父组件中设置 state 并使用 react 和 typescript 在另一个子组件中访问 state? - How to set state in parent from one child component and access the state in another child component using react and typescript? 我该如何从需要功能来设置带有react-dates状态的子组件中设置状态? - How do I set state from a child component that requires a function to set state with react-dates? 如何在父级的子级组件中设置状态? - How can I set state in a child component from a parent? 如何使用 function 将值从子组件传递到父组件? - How do i pass a value from child component to parent component using function? 如何通过在父组件中调用函数来更改子组件内的状态? - How do I change state within a child component by calling function in its parent? 如何在反应中从子组件设置父组件的 state? - How to set state of parent component from a child component in react? 如何正确设置子组件和父组件的状态? - How to set the state of the child component and the parent component correctly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM