question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How pass data from index.js with path, using redux-store?

See original GitHub issue

Sorry if my question formulating is not right. I can use example t o describe what i want. So: `I have db table where i have got paths and some other datas, i want pass dynamically path from db to routes, For example i have got this

{path: '/ato',
  action() {
    return {
      title: 'Test',
      component: <Layout><About /> <Ato /></Layout>,
    };
  },

How make it something like this?

{path: 'data.path',
      action() {
        return {
          title: 'Test',
          component: <Layout><About /> <Ato props={data} /></Layout>,
        };
      },
    },

Is there any suggestion?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
frenzzycommented, Jan 13, 2017

How about this?

const route = {
  path: '*',
  async action({ path }) {
    // get page data
    const page = await fetch(`/api/page?path=${path}`).then(resp => resp.json());
    if (!page) return null; // page not found

    // get page component `src/routes/{your-route-name}/pages/{PageComponentName}.js`
    const PageComponent = require(`./pages/${page.component}.js`).default;

    // render page
    return {
      title: page.title,
      component: <Layout><PageComponent {...page} /></Layout>,
    };
  }
}

or with code splitting

// get page component
const PageComponent = await new Promise((resolve) => {
  require.ensure([], (require) =>
    // src/routes/{your-route-name}/pages/{PageComponentName}.js
    resolve(require(`./pages/${page.component}.js`).default
  ));
});

// or
let PageComponent;
switch (page.component) {
  case 'Page1':
    PageComponent = await new Promise((resolve) => {
      require.ensure([], (require) => resolve(require('./PageComponent1').default))
    });
  case 'Page2':
    PageComponent = await new Promise((resolve) => {
      require.ensure([], (require) => resolve(require('./PageComponent2').default))
    });
  // ...
  default:
    throw new Error(`Page component ${page.component} not found.`);
}
0reactions
frenzzycommented, Jan 16, 2017
async action() {
  loadData(); // call async method and do not wait for the result (do you need this?)
  await loadData(); // call async method and wait for the result
  return {/*...*/};
}

this is the same as

action() {
  loadData(); // load data in background
  return loadData().then(() => { // load data then continue
    return {/*...*/};
  });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux Essentials, Part 4: Using Redux Data
The official Redux Essentials tutorial: learn how to work with complex Redux state in React components.
Read more >
React Router with Redux: Understanding navigation state
Use React Router to declaratively navigate within your React and Redux applications and maintain state across your app's navigation ...
Read more >
Getting Data Out of the Redux Store with Selectors
First, we call createSelector and pass in the getProducts selector and another selector called getCurrentProductId . Then, as the last parameter, we pass...
Read more >
How To Manage State in React with Redux - DigitalOcean
Redux operates according to a few concepts. First, the store is a single object with fields for each selection of data. You update...
Read more >
How to pass redux state to sub routes? - Stack Overflow
In order to inject any state or action creators into the props of a React component you can use connect from react-redux which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found