Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With getComponent how to pass props?

Tags:

react-router

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?

like image 996
Ally Avatar asked Nov 06 '15 16:11

Ally


People also ask

How do you pass props as a object?

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!

How do you pass props in Nextjs pages?

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.

How do you pass Props between components?

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.

How do you pass data with props in React native?

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!


2 Answers

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.

like image 166
taion Avatar answered Oct 21 '22 01:10

taion


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

like image 5
Dominic Avatar answered Oct 21 '22 00:10

Dominic