1

我一直试图理解为什么当从滚动视图项将数据传递给回调函数时,父组件接收到未定义的数据。我console.log在 item 组件中有一个,它记录了正确的值,但是父组件中的日志记录了undefined……remove Item 组件也成功删除了该组件,但是,该函数中的日志也返回 undefined ……此时我不确定它是我正在使用的模块还是 react-natives scrollView ...感谢您的帮助。

子项,项组件

render() {
        const {data, active } = this.props;
        const key = data.key;
        console.log(key);
        return (
            <Animated.View style={[
                styles.row,
                this._style,
            ]}>
                <Text style={styles.text}>{data.role}</Text>
                <Text style={styles.nameText}>{data.name}</Text>
                <TouchableOpacity onPress={() => {this.props.onRemove(key)}}>
                    <Text>X</Text>
                </TouchableOpacity>
                <TouchableOpacity onPress={() => {this.props.modalTwo(key)}}>
                    <Text>  E</Text>
                </TouchableOpacity>
            </Animated.View>
        );
    }

父组件函数和渲染方法

_renderRow = ({data, active}) =>{
        return <Role data={data} onRemove={() => {this.removeItem()}} modalTwo={() =>{this.openModalTwo()}}/>
    }

 removeItem = (key) => {
    const newRoleList = this.state.roleList.slice();
    console.log(key);
    const roleIndex = newRoleList.findIndex(item => item.key === key);
    console.log(key);
    newRoleList.splice(roleIndex,1);
    this.setState( {roleList: newRoleList});
}

openModalTwo = (key) => {
    console.log(key);
    const newObjectArray = this.state.roleList.slice();
    console.log('newObjectArray: ',newObjectArray);
    const editObject = newObjectArray.find(item => item.key === key );
    console.log('editObject:',editObject);
    this.setState({modalVisibleTwo:!this.state.modalVisibleTwo, editObject: editObject});
}

render(){
    reindex = (indexes, sourceArray) => indexes.map(i => sourceArray[i]); 
    return(
        <KeyboardAvoidingView
            behavior="padding"
            style={styles.listContainer}
        > 
            <Modal
                isVisible = {this.state.modalVisible}
                onSwipe = {() => {this.setState({modalVisible: !this.state.modalVisible})}}
                swipeDirection="right"
            >
                <View style={styles.modalContainer}>
                    <View style={styles.textInputContainer}>
                        <TextInput 
                            style={styles.textInput} 
                            placeholder={'Enter Role'}
                            underlineColorAndroid={'#ffffff'}
                            onChangeText={(text) => this.updateTextInput('role',text)}
                        />
                        <TextInput 
                            style={styles.textInput} 
                            placeholder={'Enter Name'}
                            underlineColorAndroid={'#ffffff'}
                            onChangeText={(text) => this.updateTextInput('name',text)}
                        />
                    </View>
                    <TouchableOpacity style={styles.buttonStyle} onPress={() => {this.state.nameInput && this.state.roleInput != '' ? this.addAnotherRole() : this.reset();}}>
                            <Text style={styles.buttonText}>{this.state.nameInput && this.state.roleInput != '' ? 'Update' : 'Close'}</Text>
                    </TouchableOpacity>
                </View>
            </Modal>
            <Modal
                isVisible = {this.state.modalVisibleTwo}
                onSwipe = {() => {this.setState({modalVisibleTwo: !this.state.modalVisibleTwo})}}
                swipeDirection="right"
            >
                <View style={styles.modalContainer}>
                    <View style={styles.textInputContainer}>
                        <TextInput 
                            style={styles.textInput} 
                            placeholder={'Enter Role'}
                            underlineColorAndroid={'#ffffff'}
                            // value={this.state.editObject.role}
                            onChangeText={(text) => this.updateTextInput('role',text)}
                        />
                        <TextInput 
                            style={styles.textInput} 
                            placeholder={'Enter Name'}
                            underlineColorAndroid={'#ffffff'}
                            // value={this.state.editObject.name}
                            onChangeText={(text) => this.updateTextInput('name',text)}
                        />
                    </View>
                    <TouchableOpacity style={styles.buttonStyle} onPress={() => {this.state.nameInput && this.state.roleInput != '' ? this.addAnotherRole() : this.reset();}}>
                            <Text style={styles.buttonText}>{this.state.nameInput && this.state.roleInput != '' ? 'Update' : 'Close'}</Text>
                    </TouchableOpacity>
                </View>
            </Modal>
            <Text style={styles.taskTitle}>Company Roles</Text>
            <Text style={styles.introBlurb}>Paragraph On how to use this component</Text>
            <View style={styles.titleContainer}>
                <Text style={styles.title}>Title</Text>
                <Text style={styles.title}>Name</Text>
            </View>
            <SortableList
                style={styles.List}
                data={this.state.roleList}
                renderRow={this._renderRow}
                // onChangeOrder ={(nextOrder) => {this.setState({ roleList: reindex(nextOrder, this.state.roleList)})}}
            />
            <TouchableOpacity style={styles.addButton} onPress={() =>{this.setState({modalVisible: !this.state.modalVisible})}}>
                <Text style={styles.addText}>Add another role</Text>
            </TouchableOpacity>
        </KeyboardAvoidingView>
    );
}

}

我正在使用的模块:https ://www.npmjs.com/package/react-native-sortable-list

4

1 回答 1

0

您没有将参数传递给_renderRow. 例如,

onRemove={() => {this.removeItem()}}

应该是:

onRemove={(key) => {this.removeItem(key)}}

要不就:

onRemove={this.removeItem}
于 2018-08-23T19:20:06.130 回答