传递给 parent 的 ScrollView 项目数据返回为未定义
ScrollView item data passed to parent is returning as undefined
我一直在试图理解为什么当从滚动视图项目向回调函数传递数据时,parent 组件接收未定义。我在项目组件中有一个 console.log
并且它记录了正确的值但是 parent 组件中的日志记录 undefined
... 删除项目组件也成功删除了该组件,但是,该函数 returns 中的日志也未定义...此时我不确定它是我正在使用的模块还是 react-natives scrollView...
感谢 any-help。
child,项目组成
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>
);
}
parent组件函数和渲染方法
_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
您没有为 _renderRow
中的回调 prop 传递参数。例如,
onRemove={() => {this.removeItem()}}
应该是:
onRemove={(key) => {this.removeItem(key)}}
或者只是:
onRemove={this.removeItem}
我一直在试图理解为什么当从滚动视图项目向回调函数传递数据时,parent 组件接收未定义。我在项目组件中有一个 console.log
并且它记录了正确的值但是 parent 组件中的日志记录 undefined
... 删除项目组件也成功删除了该组件,但是,该函数 returns 中的日志也未定义...此时我不确定它是我正在使用的模块还是 react-natives scrollView...
感谢 any-help。
child,项目组成
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>
);
}
parent组件函数和渲染方法
_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
您没有为 _renderRow
中的回调 prop 传递参数。例如,
onRemove={() => {this.removeItem()}}
应该是:
onRemove={(key) => {this.removeItem(key)}}
或者只是:
onRemove={this.removeItem}