2016-07-20 2 views
1

[React Native]의 부모 요소에있는 단추의 자식 클릭 이벤트를 처리하려하고 있습니다. 그래서 기본 반응 할 임 아주 새로운 어떤 책 실수를 용서하십시오 : 부모에게 자식 이벤트 노출

// my transparent button child 

const styles = StyleSheet.create({ 
    button: { 
     backgroundColor: 'transparent', 
     borderColor: Theme.button.borderColor, 
     borderWidth: Theme.button.borderWidth, 
     borderRadius: Theme.button.buttonRadius, 
     fontFamily: Theme.button.fontFamily, 
     fontWeight: Theme.button.fontWeight, 
     color: Theme.button.fontColor 
    } 
}) 

var handleClick = function() { 
    console.log('You clicked: '); 
} 

const TransparentButton = React.createClass({ 
    render() { 
     var boundClick = handleClick.bind(this); 
     return (
      <Button 
       style={styles.button} 
       textStyle={styles.button} 
       onPress={boundClick}> 
        {this.props.children} 
       </Button> 
     ); 
    } 
}); 

module.exports = TransparentButton; 

// and this is the snippent that is trying to catch the click event 
class Welcome extends Component { 
    render() { 
     return (
      <Page 
      style={styles.container} 
      backgroundColor={Theme.bgColor}> 

       <TransparentButton 
        handleClick={() => console.log('hello there outter')}> 
        Ryans Text Button 
       </TransparentButton> 
      </Page> 
     ) 
    } 
} 

내부 이벤트 클릭이 잘 등록하지만, outter는 이벤트가 happenes 결코

.

답변

1

TransparentButton에서는 부모 함수를 호출하지 않기 때문입니다.

const TransparentButton = React.createClass({ 
    render() { 
     return (
      <Button 
       style={styles.button} 
       textStyle={styles.button} 
       onPress={this.props.handleClick}> 
        {this.props.children} 
       </Button> 
     ); 
    } 
}); 

ES6의 방법은 거의 동일하며 ES5와 ES6 혼합하지, 코드를 통해 일치하는 것이 좋습니다 :

export default TransparentButton extends Component{ 
    render() { 
     return (
      <Button 
       style={styles.button} 
       textStyle={styles.button} 
       onPress={this.props.handleClick}> 
        {this.props.children} 
       </Button> 
     ); 
    } 
}; 
+0

대단히 감사합니다. – TheMan68

+0

방금 ​​나가기 위해 이것이 최선의 방법인지 알고 싶습니까? 어떤 것이 더 좋을까요? 이것을 creatClass로 설정하거나 버튼 자체를 확장하려면? – TheMan68

+0

그리고 버튼을 확장 한 경우 TransParentButton 렌더링 함수에서 상위 버튼을 클릭하는 등으로 버튼 뷰를 부모에게 반환하는 방법은 무엇입니까? – TheMan68

관련 문제