【问题标题】:function execute issue in react native功能在本机反应中执行问题
【发布时间】:2020-02-04 12:04:52
【问题描述】:

亲爱的朋友们,这是我的 React Native 根据:

 renderAccordionContentBasic() {
        const { selectedItem } = this.state;
        return <View>

            <View style={Styles.formPicker}>                
                <Picker
                    selectedValue={this.state.gender}
                    onValueChange={(itemValue, itemIndex) => this.setState({ gender: itemValue })}  style={Styles.pickerText}>                    
                    <Picker.Item label="مرد" value="true" style={Styles.pickerText} />
                    <Picker.Item label="زن" value="false"  style={Styles.pickerText} />
                </Picker>
            </View>
            <View style={Styles.col}>
                <TextInput onChange={(itemValue, itemIndex) => this.setState({ bankName: itemValue })} value={this.state.bankName} style={Styles.textInputHalf} placeholder={'نام بانک'} />
                <TextInput onChange={(itemValue, itemIndex) => this.setState({ bankSheba: itemValue })} value={this.state.bankSheba} style={Styles.textInputHalf} placeholder={'شماره 16 رقمی شبای بانکی'} />
            </View>
            <TextInput style={Styles.textInputMulti} value={this.state.aboutus} onChange={(itemValue, itemIndex) => this.setState({ aboutus: itemValue })} multiline={true} numberOfLines={10} placeholder={'رزومه و سوابق'} />

            <Button style={Styles.btn} **onPress={this.handleBasic(1)}**>
                <Text style={Styles.formBtnText}>{'ذخیره'.toUpperCase()}</Text>
                <Icon active name='content-save' type="MaterialCommunityIcons" style={Styles.formBtnIcon} />
            </Button>
        </View>
    }

这是我在按下按钮时执行的功能:

handleBasic =(id)=>{
        alert(id)

    }

现在当我的表单(组件)挂载 handleBasic() 执行但当我按下按钮时 handleBasic() 没有执行 请帮帮我

【问题讨论】:

  • 为什么onPress 包裹在** 中?
  • 我清除了 onPress 后面的**

标签: reactjs react-native jsx


【解决方案1】:

把你的onPress改成这样

onPress={() => this.handleBasic(1)}

通过像onPress={this.handleBasic(1)}这样调用,组件渲染时会立即调用handle方法。所以,当你按下按钮时它不会被调用。

另外,不要忘记将您的renderAccordionContentBasic() 绑定到您的班级。您可以使用箭头函数或.bind(this)

【讨论】:

  • 如果我的回答可以解决您的问题,请将此答案隐藏为正确答案,以帮助其他人更轻松地找到此解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 2021-01-16
  • 2022-01-21
  • 2017-11-19
  • 2018-12-14
相关资源
最近更新 更多