【问题标题】:How to re-render a specific component in react native using reference?如何使用参考在本机反应中重新渲染特定组件?
【发布时间】:2020-05-26 13:15:21
【问题描述】:

我必须在按下清除/关闭图标时清除或重新呈现 DatePicker 输入。但是 DatePicker 中没有明确的选项。所以我必须重新渲染特定组件(DatePicker)而不是整个 UI。

import DatePicker from 'react-native-date-ranges';

<View style={styles.row}>
    <Item rounded style={styles.inputWrap}>
        <Input placeholder='Customer Id' keyboardType="numeric" placeholderTextColor='rgba(0,0,0,0.4)' onChangeText={(text) => this.setState({customerCode: text})}/>
    </Item>
    <View style={{flex: .025}}></View>
    <Item rounded style={styles.inputWrap}>
        <Input placeholder='Invoice No' keyboardType="numeric" placeholderTextColor='rgba(0,0,0,0.4)' onChangeText={(text) => this.setState({invoiceNo: text})}/>
    </Item>
    <DatePicker
        style={ {height: 36, borderRadius: 8, borderColor: "#cccccc", borderWidth: 1,} }
        customStyles = { {
            placeholderText:{ fontSize:14 }, // placeHolder style
            headerStyle : { backgroundColor:'#007aff' },            // title container style
            headerMarkTitle : { }, // title mark style 
            headerDateTitle: { }, // title Date style
            contentInput: { fontSize:14 }, //content text container style
            contentText: {fontSize:14}, //after selected text Style
        } } // optional 
        ButtonStyle={{backgroundColor:'#007aff', borderWidth:1, borderRadius:8,marginHorizontal: 20, borderColor:'#fff'}}
        ButtonTextStyle={{color: '#fff',alignSelf:'center',padding:10, fontSize: 16}}
        centerAlign // optional text will align center or not
        allowFontScaling = {false} // optional
        markText={'Select Date'}
        ButtonText='Select'
        placeholder={'Ex: Apr 27, 2018 → Jul 10, 2018'}
        mode={'range'}
        onConfirm={(text) => {this.setState({invoiceDate: text}); console.log('invoiceDate: ', text)}}
        ref = {(ref)=> this.picker = ref}
    />
    <Icon style={{padding: 10, marginLeft: -30, marginTop: -2}} size={20} name={'md-close'} color={'red'} onPress={() => "Clear or rerender the DatePicker Input. There are no clear option in DatePicker. So i have to rerender"}/>
</View>

我是 react-native 的新手。所以,我非常需要你的帮助

【问题讨论】:

    标签: react-native rerender


    【解决方案1】:

    您使用的库缺少一些用于设置所选日期的道具。

    改用https://github.com/wix/react-native-calendars

    【讨论】:

    • 谢谢兄弟。但我需要日期游侠。所以我使用了 react-native-date-ranges。我想知道如何使用引用重新渲染特定组件。
    • @Md.RobiUllah,如果您阅读文档,您可以使用Period marking
    • 好的。我正在查。但是你知道如何重新渲染一个特定的组件吗?如果是,请与我分享
    • 组件仅在 propsstate 更改时重新呈现。但是,您可以通过在类组件上调用 this.forceUpdate 来强制它重新呈现
    • 是否可以重新渲染为新组件。和 forceUpdate 一样,输入框也会被清空。
    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多