I can't figure out how to pass props to the component. This is important as I don't want to fetch data in the componentDidMount method as it will then be invisible to search engines.
My code looks like this
const router =
<Route path="/" component={App}>
<IndexRoute onEnter={redirectToLogin} component={LandingPage} />
<Route path="panel" component={ ControlPanel }>
...
</Route>
<Route
path="/:handle"
onEnter={maybeRedirectToHome}
getComponent={(location, callback)=> {
getSiteHandleByName(location.pathname.slice(1))
.then(function(handle){
if (handle){
callback(null, Portfolio)
} else {
callback(null, NotFound)
}
})
.catch(callback)
}}
getChildRoutes={(location, callback)=> {
callback(null, portfolioRoutes)
}}
/>
</Route>
I'm trying to serve up a portfolio React App when the user visits a valid url like mysite.com/thishandleisvalid
but I'll need to also fetch all the content for that app at the getComponent
point and pass it in as a property. E.g. you might normally do something like this <Portfolio contentItems={fetchedItems} />
.
Is doing this possible?
Use the spread syntax (...) to pass an object as props to a React component, e.g. <Person {... obj} /> . The spread syntax will unpack all of the properties of the object and pass them as props to the specified component. Copied!
Props are passed from a parent component to its child components. What you are asking is to pass props between sibling components (ie. page) which I don't think has a straightforward way to do so. Maybe you can try using React's Context API so that your Test2 is not using any hard-coded value.
Passing Props In order to pass a prop to a component all we have to do is name the prop and set it equal to some value. In the example above, we are passing a prop called name that is equal to a string. Passing a prop gives us access to the information in our Greeting component.
Just call an alert method in the childToParent function and pass that function as a prop to the child component. And in the child component, accept the childToParent function as a prop. Then assign it to an onClick event on a button. That's it!
This is really easy to do with stateless components. Just do something like:
function getComponent(location, callback) {
const Component = /* ... */;
const items = /* ... */;
callback(null, props => <Component {...props} items={items} />);
}
The reason we don't explicitly support this sort of pattern is because it's fairly atypical to wire things up this way - for apps that need to deal with this sort of thing, it's much more common to use the onEnter
hook to e.g. populate a Flux store, then connect the components as appropriate to the relevant stores.
Also if you don't mind accessing props under route
, you can pass props like this:
JSX:
<Route
path="route_path"
customProp="hello"
getComponent={(location, cb) => {
// ...
}}
Programatically:
childRoutes: [
{
path: 'route_path',
customProps: 'hello',
getComponent(location, cb) {
// ....
},
And customProp
will be available via props.route.customProp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With