简体   繁体   English

将'this'绑定到NavigationBarRouteMapper非函数

[英]Bind 'this' to NavigationBarRouteMapper non-function

I have a NavBarRouteMapper object that I pass to my navbar. 我有一个NavBarRouteMapper对象,我传递给我的导航栏。 However, in the onpress function of one of the buttons I need to access the state, but I'm not sure how to bind 'this' to the object, since it is a non-function. 但是,在其中一个按钮的onpress功能中我需要访问状态,但是我不知道如何将'this'绑定到对象,因为它是一个非函数。 Relevant code as follows 相关代码如下

 class app extends Component {
   state: {
     sideMenuIsOpen: boolean,
   };
   constructor(props: Object) {
      super(props);
     this.state = {
       sideMenuIsOpen: false,
     };
   };

static NavigationBarRouteMapper = {
     LeftButton(route, navigator, index, navState) {
       if (index > 0) {
         return (
           <SimpleButton
            // TODO: Make this work, Menu button needs to go to this
            // The problem is here. this.state is undefined
             onPress={console.log(this.state)}
             customText="Back"
             style={styles.navBarLeftButton}
             textStyle={styles.navBarButtonText}
           />
         );
       }
     },
     RightButton(route, navigator, index, navState) {
       // TODO change add button for admins
       switch (route.title) {
         case "Login":
          return null;
         default:
           return (
             <SimpleButton
               onPress={() => {
                 navigator.push({
                   title: "Profile",
                   component: Profile,
                 });
               }}
               customText="Add"
               style={styles.navBarRightButton}
               textStyle={styles.navBarButtonText}
             />
          );
       }
     },
     Title(route, navigator, index, navState) {
       return (
         <Text style={styles.navBarTitleText}>{route.title}</Text>
       );
     },
   };


render() {
     return (
       <SideMenu
         menu={<Menu navigate={this.navigate} />}
         isOpen={this.state.sideMenuIsOpen}
        >
         <Navigator
           ref="rootNavigator"
           initialRoute={{
             title: "Login",
             component: LoginScene,
             navigator: this.refs.rootNavigator,
           }}
           renderScene = {this.renderScene}

           navigationBar={
             <Navigator.NavigationBar
              // Since this is an object, I can't bind 'this' to it,
              // and the documentation calls for it to be passed an object
              routeMapper={app.NavigationBarRouteMapper}
              style={styles.navBar}
              />
           }
        />
      </SideMenu>
    );
  };

} }

So you're trying to return the parent's state from the child onClick? 所以你试图从孩子onClick返回父母的状态? If so you can add an onClick which calls the parent onClick that you can pass to the child via props. 如果是这样,你可以添加一个onClick调用父onClick,你可以通过道具传递给孩子。

var Hello = React.createClass({
  getInitialState: function() {
    return {test: "testing 1 2 3"};
  },
  clickMethod: function () {
    alert(this.state.test);
  },
  render: function() {
    return <div onClick={this.clickMethod}>Hello {this.props.name}</div>;
  }
});

var Child = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

https://jsfiddle.net/reactjs/69z2wepo/ https://jsfiddle.net/reactjs/69z2wepo/

Also if you had a list of components where each component needs to pass a unique piece of data to the parent you can do so using bind. 此外,如果您有一个组件列表,其中每个组件需要将唯一的数据传递给父组件,您可以使用bind执行此操作。

var Hello = React.createClass({
  getInitialState: function() {
    return {test: "testing 1 2 3"};
  },
  clickMethod: function (argument) {
    alert(argument);
  },
  render: function() {
    return <div onClick={this.clickMethod.bind(this, "custom argument")}>Hello {this.props.name}</div>;
  }
});

var Child = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

ReactDOM.render(
  <Hello name="World" />,
  document.getElementById('container')
);

https://jsfiddle.net/chrshawkes/64eef3xm/1/ https://jsfiddle.net/chrshawkes/64eef3xm/1/

暂无
暂无

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

相关问题 函数声明不能​​嵌套在非函数块中 - Function declarations cannot be nested within non-function blocks 在函数调用之前执行非函数代码 - Execute non-function code before a function call 非函数 Javascript 代码块中的返回值 - Return value in non-function Javascript code block 为什么可以将非函数参数传递给 Promise.then() 而不会导致错误? - Why is it possible to pass in a non-function parameter to Promise.then() without causing an error? 如何根据oninput range滑块的变化调整非功能性Javascript小部件中的属性? - How can I adjust the properties in a non-function Javascript widget based on oninput range slider changes? 如何取消声明通过JavaScript控制台添加的所有非函数变量(无需刷新) - How to UN-declare all non-function variables added via JavaScript console (without refreshing) 是否可以使用打字稿映射类型来制作接口的非功能属性类型? - Is it possible to use typescript mapped types to make a type of non-function properties of an interface? 是否可以在jQuery插件的非功能参数参数中访问DOM元素“ this”? - Is it possible to access the DOM element 'this' in a non-function parameter argument in a jQuery plugin? ES6 箭头函数与 ES5:如何知道使用 ES5 非箭头 function 时将“this”绑定到哪个 function - ES6 Arrow functions vs ES5: how to know which function to bind `this` to when using ES5 non-arrow function 如何不影响当前路线并直接更改NavigationBarRouteMapper的“标题”(React Native)? - How to not affect current route and change 'Title' of NavigationBarRouteMapper directly — React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM