[英]how can i call react state different js
i want to change state different js but i can not , i have a sidebar.js with react-burger-menu 我想更改状态不同的js,但我不能,我有一个带有react-burger-menu的sidebar.js
i want to call and change toggleMenu state in header.js 我想调用并更改header.js中的toggleMenu状态
When I click the menu link, i want to toggle react-burger-menu but different js. 当我单击菜单链接时,我想切换react-burger-menu但使用不同的js。 this is not working. 这是行不通的。
sidebar.js sidebar.js
import React from "react";
import PropTypes from "prop-types";
import { reveal as Menu } from "react-burger-menu";
import * as FontAwesome from "react-icons/lib/fa";
export default class SidebarMenu extends React.Component {
constructor (props) {
super(props)
this.state = {
menuOpen: false
}
}
handleStateChange (state) {
this.setState({menuOpen: state.isOpen})
}
closeMenu () {
this.setState({menuOpen: false})
}
toggleMenu () {
this.setState({menuOpen: !this.state.menuOpen})
}
render () {
return (
<div>
<Menu
isOpen={this.state.menuOpen}
onStateChange={(state) => this.handleStateChange(state)}
>
// menu content
</Menu>
</div>
</div>
)
}
}
header.js have link for react-burger-menu header.js具有react-burger-menu的链接
import React from 'react';
import PropTypes from 'prop-types';
import SidebarMenu from "../SidebarMenu";
export default class Header_Video extends React.Component {
render() {
return (
<Container>
<Row>
<Col md={5} sm={12} xs={12} className="text-left mobile-right">
<div className="bar__module">
<a onClick={this.toggleMenu}>Menu</a>
</div>
</Col>
</Row>
</Container>
);
}
}
thanks for help 感谢帮助
note: i have a app.js all files import. 注意:我有一个app.js所有文件导入。 I want to run toggleMenu in header.js 我想在header.js中运行toggleMenu
app.js app.js
const TemplateWrapper = ({ children }) => (
<div id="outer-container">
<SidebarMenu />
<main id="page-wrap" className="page-wrap">
<HeaderVideo /> {children()}
<Footer />
</main>
</div>
);
menuOpen
should be in a parent state of both components. menuOpen
应该处于两个组件的父状态。
Example: 例:
class App extends React.Component {
constructor (props) {
super(props)
this.state = {
menuOpen: false
}
}
closeMenu = () => {
this.setState({menuOpen: false})
}
toggleMenu = () => {
this.setState({menuOpen: !this.state.menuOpen})
}
render() {
return (
<div>
<SidebarMenu isMenuOpen={this.state.menuOpen} toggleMenu={this.toggleMenu} />
<Header toggleMenu={this.toggleMenu} />
</div>
)
}
}
You may have further errors beyond just this, but the glaring error to me is that toggleMenu() is not bound to the constructor. 您可能不仅会遇到其他错误,但是对我来说,明显的错误是toggleMenu()未绑定到构造函数。
https://reactjs.org/docs/handling-events.html https://reactjs.org/docs/handling-events.html
try: 尝试:
import React from "react";
import PropTypes from "prop-types";
import { reveal as Menu } from "react-burger-menu";
import * as FontAwesome from "react-icons/lib/fa";
export default class SidebarMenu extends React.Component {
constructor (props) {
super(props)
this.state = {
menuOpen: false
}
this.toggleMenu = this.toggleMenu.bind(this);
// the above statement binds the function to the object constructor
}
handleStateChange (state) {
this.setState({menuOpen: state.isOpen})
}
closeMenu () {
this.setState({menuOpen: false})
}
toggleMenu () {
this.setState({menuOpen: !this.state.menuOpen})
}
render () {
return (
<div>
<Menu
isOpen={this.state.menuOpen}
onStateChange={(state) => this.handleStateChange(state)}
>
// menu content
</Menu>
</div>
</div>
)
}
}
You'll also want to use an HTML5 button tag instead of a link tag, the correct HTML semantic structure provides a bunch of underlying features and greatly improves accessibility out of the box. 您还需要使用HTML5按钮标签而不是链接标签,正确的HTML语义结构提供了许多基础功能,并大大提高了可访问性。
Also, remove the arrow function and pass a reference to the function, not the returned value. 此外,删除箭头函数,并传递对该函数的引用,而不是返回值。 This is so react doesn't call the function immediately but stores the function reference to execute upon the click event. 因此,react不会立即调用该函数,而是存储要在click事件上执行的函数引用。
<button onClick={this.toggleMenu}>Menu</button>
// instead of
<a onClick={() => this.toggleMenu()}>Menu</a>
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.