Javascript 无法获取子钩子以接收状态
我希望能够将当前用户从我的Javascript 无法获取子钩子以接收状态,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我希望能够将当前用户从我的Comment组件传递到我的CommentList组件 class Comment extends Component { render() { return( <View> <Header rounded style={{ backgroundC
Comment
组件传递到我的CommentList
组件
class Comment extends Component {
render() {
return(
<View>
<Header
rounded
style={{
backgroundColor: '#ffffff',
position: 'relative',
}}
>
<View style={{flexDirection: 'row', flexWrap: 'wrap', right: '43%', top: '50%'}}>
<Icon name='chevron-left' size={10} color='#006FFF' style={{top: '6%'}}/>
<NativeText
onPress={() => this.props.history.push('/')}
style ={{color: '#006FFF', fontSize: 12, fontFamily: 'Montserrat-Regular'}}
>
Back
</NativeText>
</View>
</Header>
<View
style={{paddingLeft: '2%', paddingTop: '2%'}}
>
<CommentList
options={this.props.location.state.comments}
currentUser={this.props.location.state.currentUser}
/>
</View>
</View>
)
}
}
export default withRouter(Comment)
const CommentList = (options, currentUser) => {
const [modalVisible, setModalVisible] = useState(false)
const [parentId, changeParentId] = useState('')
const [commentInput, changeComment] = useState('')
return (
<View>{console.log(currentUser)}
{options.options.map(option => (
<View>
<NativeText
style={{fontSize: 12, fontFamily: 'Montserrat-Regular'}}
>
{option.content}
</NativeText>
<Icon
name='reply'
size={12}
onPress={() => {
setModalVisible(true)
changeParentId(option._id)
}}
/>
{
<View
style={{left: '10%'}}
>
<CommentList
options={option.reply}
/>
</View>
}
</View>
))}
</View>
)
}
类注释扩展组件{
render(){
返回(
this.props.history.push('/')}
样式={{color:'#006FFF',字体大小:12,字体系列:'Montserrat Regular'}
>
返回
)
}
}
使用路由器导出默认值(注释)
const CommentList=(选项,当前用户)=>{
const[modalVisible,setModalVisible]=useState(false)
const[parentId,changeParentId]=useState(“”)
常量[commentInput,changeComment]=useState(“”)
报税表(
{console.log(currentUser)}
{options.options.map(option=>(
{option.content}
{
setModalVisible(真)
changeParentId(选项。\u id)
}}
/>
{
}
))}
)
}
当我尝试将它传递给
currentUser
时,它返回一个空对象<代码>选项确实显示在注释列表中
您缺少注释列表的对象解构语法。其内容应如下:
const CommentList = ({ options, currentUser }) => {
// component content
}
传递给functional component的道具将合并到一个对象中。因此,您需要对值进行分解 或者
const CommentList=(道具)=>{
//props.options//props.currentUser
}
您如何使用评论组件以及如何向其传递道具?我正在通过react路由器传递它。我知道该信息存在于this.props.location.state.currentUser中,并且具有当前用户信息。它只是不存在于CommentList中。currentUser是异步数据吗?您应该将CommentList
的定义更改为类似const CommentList=({options,currentUser})=>{
@schoenbl您是否尝试过注销注释呈现方法顶部的this.props.location.state.currentUser
?