Reactjs can';无法将参数从链接传递到组件

Reactjs can';无法将参数从链接传递到组件,reactjs,react-native,react-native-router-flux,Reactjs,React Native,React Native Router Flux,我在主组件中有此链接: <Link to={'/protected/'+this.state.username } //params={{ username: this.state.user }} style={styles.navItem_login} underlayColor="#f0f4f7"> <Text style={styles.navItem_login_text}>Protected Page</Text&g

我在主组件中有此链接:

<Link
    to={'/protected/'+this.state.username }
    //params={{ username: this.state.user }}
    style={styles.navItem_login}
    underlayColor="#f0f4f7">
    <Text style={styles.navItem_login_text}>Protected Page</Text>
</Link>

保护页
然后在App.js中是路线:

<PrivateRoute path="/protected" component={Protected} />


const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={props => (
    fakeAuth.isAuthenticated ? (
      <Component {...props}/>
    ) : (
      <Redirect to={{
        pathname: '/login',
        state: { from: props.location }
      }}/>
    )
  )}/>
)

const Protected = () => <Text style={styles.header}>{this.props.username}</Text>

const privaterout=({component:component,…rest})=>(
(
伪造的。是否已验证(
) : (
)
)}/>
)
const Protected=()=>{this.props.username}
但是
this.props.username
返回
undefined
,以及ass
props.match.params.value
道具位置


如何在组件中获取传递的参数?。谢谢

因为您的链接应该导航到
/protected/:username
,所以您需要在相同的路径上呈现受保护的组件

链接

<Link
    to={'/protected/'+this.state.username }
    //params={{ username: this.state.user }}
    style={styles.navItem_login}
    underlayColor="#f0f4f7">
    <Text style={styles.navItem_login_text}>Protected Page</Text>
</Link>

您的链接路径是
'/protected/:username'
,但受保护的组件在
/protected
处呈现,因此参数应该是未定义的。我也有这个问题:可能您可以帮助;)谢谢!!
<PrivateRoute path="/protected/:username" component={Protected} />


const PrivateRoute = ({ component: Component, ...rest }) => (
  <Route {...rest} render={props => (
    fakeAuth.isAuthenticated ? (
      <Component {...props}/>
    ) : (
      <Redirect to={{
        pathname: '/login',
        state: { from: props.location }
      }}/>
    )
  )}/>
)
const Protected = (props) => <Text style={styles.header}>{props.match.params.username}</Text>