2

Now I am define route "react-router-dom": "^5.1.1", like this:

const Channel = loadable(() => import(/* webpackChunkName: 'about' */ '@/views/App/Cruise/Channel'))


const routes = [
    { path: '/index', exact: true, name: 'Index', component: Channel, auth: [1] }
]

for the old way, I am pass props into component like this way:

class App extends React.Component {
    render() {
        return (<Router>
            <Switch>
                <Route path='/login' render={(props) => <Login user={this.props.user} />}/>
            </Switch>
        </Router>)
    }
}

how to pass props into component Channel when define route as an array? I have tried this way:

{ 
        path: '/app/cruise/channel', 
        exact: false, 
        name: 'Channel', 
        render: (props) => (<Channel {...props} channel={this.props.channel}/>)
    },

but it could not work. Now I am passed the props like this in the routes outer component:

<Content className='content'>
                        <Switch>
                            {routes.map(item => {
                                return (
                                    <Route
                                        key={item.path}
                                        path={item.path}
                                        exact={item.exact}
                                        render={props =>
                                            !auth ? (
                                                <item.component {...props} channel = {this.props.channel}/>
                                            ) : item.auth && item.auth.indexOf(auth) !== -1 ? (
                                                <item.component {...props} channel = {this.props.channel} />
                                            ) : (
                                                <Redirect to='/404' {...props} />
                                            )
                                        }></Route>
                                )
                            })}
                            <Redirect to='/404' />
                        </Switch>
                    </Content>

it could work but I think it is a very ugly way, any better suggestion?

2
  • Are you trying to access URL params or pass custom params? Commented Apr 11, 2021 at 7:20
  • I want to pass data through props @TJ Commented Apr 11, 2021 at 7:34

1 Answer 1

1

I believe you would use the Link component to route to the correct location. So you could use routes and Link as below

 const Channel = loadable(() => import(/* webpackChunkName: 'about' */ '@/views/App/Cruise/Channel'))


const routes = [
    { path: '/index', exact: true, name: 'Index', component: Channel, auth: [1] }
]

Link in APP

const App=(props)=>{

return (
<Link to={{
              pathname:'/index',
              user:props.user
            }} >Channel
</Link>
)

}

Inside channel you can access user from props.location.user

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.