undefined不是一个函数(评估' _this4.changeVisibilityFilterModal(true))

时间:2018-04-21 18:53:47

标签: javascript react-native ecmascript-6

我有以下代码:

static navigationOptions = {
    headerTitle: 'Screws',
    title: 'Screws',
    headerRight:
        <TouchableOpacity onPress={() => {
            this.changeVisibilityFilterModal(true);
        }}>
            <Image style={{marginRight:10}} source={require('../../assets/img/icFilter.png')}/>
        </TouchableOpacity>,
};

changeVisibilityFilterModal(visibility){
    setState({filterModalVisible: visibility});
}

问题是我无法访问 navigationOptions 中的 changeVisibilityFilterModal 。任何人都知道我怎么能够调用该方法并解释为什么它不能像现在这样工作!

2 个答案:

答案 0 :(得分:1)

您无法直接从静态方法调用changeVisibilityFilterModal,因为它是class method

static navigationOptions = ({navigation}) => ({
    headerTitle: 'Screws',
    title: 'Screws',
    headerRight:
        <TouchableOpacity onPress={() => {
            navigation.state.params.visibilityFunc(true);
        }}>
            <Image style={{marginRight:10}} source={require('../../assets/img/icFilter.png')}/>
        </TouchableOpacity>,
})

componentDidMount() {
      this.props.navigation.setParams({ visibilityFunc: this.changeVisibilityFilterModal });
    }

changeVisibilityFilterModal = (visibility) => {
    this.setState({filterModalVisible: visibility});
}

答案 1 :(得分:0)

将changeVisibilityFilterModal更改为:

changeVisibilityFilterModal = (visibility) => {
    setState({filterModalVisible: visibility});
}

所以你不必绑定它。