【问题标题】:can i share React user post length to other component我可以将 React 用户帖子长度分享给其他组件吗
【发布时间】:2021-06-20 11:44:50
【问题描述】:

我有一个用于发布页面的组件,它有, const [posts , setPosts] = useState([]); console.log(posts) 可以看到值为 11 个帖子,我想要在另一个组件中显示帖子长度,其中包含用户信息、包含所有帖子的用户卡、关注者等.我是新手,正在使用 Redux-toolkit

【问题讨论】:

  • 为什么posts数组存放在posts页面而不是redux store中?

标签: reactjs redux react-hooks use-state


【解决方案1】:
function YourComponentPostPage(props){
   const [posts, setPosts] = useState([]) //this is an empty array when first render
   return (
     <AnotherComponentWithUserInfo posts = {posts} />
   )
}
function AnotherComponentWithUserInfo(props){
  render(
      <View><Text>{props.posts}</Text></View>
  )
}

如果你的父组件有一个值并且你想把它传递给一个子组件,语法就是上面的代码。 在父组件中必须从 Redux 存储中获取值,您需要使用 react-redux 包中的 {connect} 函数,但我不认为是您要询问的情况

从这里开始的好链接:https://reactnative.dev/docs/intro-react

这里是关于 Props 的文档链接:https://reactnative.dev/docs/props

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多