gpt4 book ai didi

javascript - React JS + Material UI 抛出 leftNav 未定义

转载 作者:行者123 更新时间:2023-11-27 23:37:36 25 4
gpt4 key购买 nike

我尝试使用 Material UI 实现 React JS,但遇到了左侧导航栏切换行为的问题。

我已使用 Yeoman 命令创建了组件。

有2个组件

BodyComponent - 父级

HeaderCompoent-子级

在 Body 组件中,我创建了 menuItem 和 LeftNavBar,在 HeaderComponent 中,我使用 onLeftIconButtonTouchTap={this._handleTouch} 事件创建了 AppBar,但是当我单击汉堡包图标时,它会抛出 navLeft is undefined 。

根据我的理解,这里的问题是在子级上调用事件,而 LeftNavBar 的引用在父级中,不知何故我无法访问它。

我读到这些问题都可以解决,但是将所有组件放在一起而不是分开,但我不想将其分开,我也不想使用助焊剂来解决这一问题。

有什么好的解决办法吗?或者我必须改变我编写代码的方式吗?如果是,怎么做?

主体组件

/*jshint esnext: true */
'use strict';

import React from 'react';
import mui from 'material-ui';
import Header from './HeaderComponent';

require('styles//Body.sass');

const LeftNav = require('material-ui/lib/left-nav');
const Tabs = require('material-ui/lib/tabs/tabs');
const Tab = require('material-ui/lib/tabs/tab');
const MenuItem = mui.MenuItem;

// injectTapEventPlugin = require("react-tap-event-plugin");
// injectTapEventPlugin();


var menuItems = [{
route: 'device',
text: 'Device'
}, {
type: MenuItem.Types.SUBHEADER,
text: 'xyz'
}, {
route: 'xyz1',
text: 'xyz1'
}, {
route: 'xyz2',
text: 'xyz2'
}, {
route: 'xyz3',
text: 'xyz3'
}, {
type: MenuItem.Types.LINK,
payload: 'https://github.com/callemall/material-ui',
text: 'GitHub'
}, {
text: 'Disabled',
disabled: true
}, {
type: MenuItem.Types.LINK,
payload: 'https://www.google.com',
text: 'Disabled Link',
disabled: true
}];


class BodyComponent extends React.Component {

render() {
return (
< div className = "body-component" >
< LeftNav ref = "leftNav"
header = { < Header / > }
docked = { true }
menuItems = { menuItems }/>
< Tabs >
< Tab label = "Item One" >
(Tab content...)
< /Tab>
< Tab label = "Item Two" >
(Tab content...)
< /Tab>
< Tab label = "Item Three"
route = "home"
onActive = {
this._handleTabActive
}>
</ Tab>
< /Tabs>
< /div>
);
}
}

BodyComponent.displayName = 'BodyComponent';

// Uncomment properties you need
// BodyComponent.propTypes = {};
// BodyComponent.defaultProps = {};

export default BodyComponent;

标题组件

/*jshint esnext: true */
'use strict';

import React from 'react';
import Body from './BodyComponent';
import mui from 'material-ui';
const AppBar = require('material-ui/lib/app-bar');

//import ActionCreator from '../actions/AppBarActionCreators';
const injectTapEventPlugin = require("react-tap-event-plugin");
injectTapEventPlugin();


require('styles//Header.sass');


class HeaderComponent extends React.Component {

_handleTouch() {
console.log(this);
this.refs.leftNav.toggle();
}

render() {
return (
<div className="header-component">
< AppBar title = "vEDM"
onLeftIconButtonTouchTap={this._handleTouch}
iconClassNameRight = "muidocs-icon-navigation-expand-more" / >
</div>
);
}
}
HeaderComponent.displayName = 'HeaderComponent';

export default HeaderComponent;

最佳答案

BodyComponent 中,您可以创建一个函数来切换 leftNav 并将其作为 Header 中的 props 传递

正文

class BodyComponent extends React.Component {

toggleLeftNav() {
this.refs.leftNav.toggle();
}

render() {
return (
< div className = "body-component" >
< LeftNav ref = "leftNav"
header = { < Header toggleLeftNav={ this.toggleLeftNav.bind(this) } /> }
docked = { true }
menuItems = { menuItems }/>
< Tabs >
< Tab label = "Item One" >
(Tab content...)
< /Tab>
< Tab label = "Item Two" >
(Tab content...)
< /Tab>
< Tab label = "Item Three"
route = "home"
onActive = {
this._handleTabActive
}>
</ Tab>
< /Tabs>
< /div>
);
}
}

标题

class HeaderComponent extends React.Component {

_handleTouch() {
this.props.toggleLeftNav();
}

render() {
return (
<div className="header-component">
< AppBar title = "vEDM"
onLeftIconButtonTouchTap={this._handleTouch.bind(this)}
iconClassNameRight = "muidocs-icon-navigation-expand-more" / >
</div>
);
}
}

关于javascript - React JS + Material UI 抛出 leftNav 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33998559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com