简体   繁体   English

单击按钮后如何将道具推到新页面?

[英]How to push props to a new page on button click?

I currently have a table and each cell has a button. 我目前有一张桌子,每个单元格都有一个按钮。 Upon clicking the button, based on that particular day (Monday or Tuesday), class (class 1 or class 2), and name (Kev or Josh), how can I push an object related to that particular button in the table to a new page? 单击按钮后,基于该特定日期(星期一或星期二),班级(第1班或第2班)和名称(Kev或Josh),如何将与表格中该特定按钮相关的对象推到新的页? Using, ReactJS + React Router + Redux, what would be the correct approach to this? 使用ReactJS + React Router + Redux,什么是正确的方法?

And when once navigated to the new page, the new page would then populate a table with the class information from the object passed in, related to button cell clicked. 当导航到新页面时,新页面将使用来自传入对象的类信息填充表,该类信息与单击的按钮单元有关。

Code: 码:

http://jsfiddle.net/k7wbzc4j/16/ http://jsfiddle.net/k7wbzc4j/16/

<table className="test-table">
  <thead>
    <tr>
      <th>List</th>
      <th colSpan="2">Monday</th>
      <th colSpan="2">Tuesday</th>
    </tr>
    <tr>
      <th>Names</th>
      <th>Class 1</th><th>Class 2</th>
      <th>Class 1</th><th>Class 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Kev</td>
      <td><button>abc</button></td><td><button>def</button></td>
      <td><button>ghi</button></td><td><button>jkl</button></td>
    </tr>
    <tr>
      <td>Josh</td>
      <td><button>mno</button></td><td><button>pqr</button></td>
      <td><button>stu</button></td><td><button>vwx</button></td>
    </tr>
  </tbody>
</table>

Will accept and upvote answer. 会接受并支持答案。 Thank you in advance 先感谢您

EDIT 编辑

Should the table rows data be structured like so and reference the object based on ID like the following? 表行数据是否应该像这样构造并基于ID引用对象,如下所示? If so how can I locate that particular id object based on the cell location (with day, name, and class number taken into account)? 如果是这样,我如何根据单元格位置(考虑了日期,名称和班级号)找到那个特定的id对象?

list: [
  {
    name: Kev
      monday: {
        class1: {
          id: 0,
          classTitle: abc,
          number: class1,
          info: {
            time: 1,
            classSize: 2,
          }
        },
        class 2: {
          id: 1,
          classTitle: def,
          number: class2,
          info: {
            time: 1,
            classSize: 2,
          }
        }
      },
      tuesday: {
        class1: {
          id: 2,
          classTitle: ghi,
          number: class1,
          info: {
            time: 1,
            classSize: 2,
          }
        },
        class 2: {
          id: 3,
          classTitle: jkl,
          number: class2,
          info: {
            time: 1,
            classSize: 2,
          }
        }
      },
  },

  {
    name: Josh, 
      monday: {
        class1: {
          id: 4,
          classTitle: mno,
          number: class1,
          info: {
            time: 1,
            classSize: 2,
          }
        },
        class2: {
          id: 5,
          classTitle: pqr,
          number: class2,
          info: {
            time: 1,
            classSize: 2,
          }
        }
      },
      tuesday: {
        class1: {
          id: 6,
          classTitle: stu,
          number: class1,
          info: {
            time: 1,
            classSize: 2,
          }
        },
        class2: {
          id: 7,
          classTitle: vwx,
          number: class2,
          info: {
            time: 1,
            classSize: 2,
          }
        }
      },
    }
  }
]

your JSFiddle doesn't use react-router or react-redux so nobody will be able to point to one particular place of your code and tell you what exactly to do differently. 您的JSFiddle不使用react-router或react-redux,因此没有人能够指向您代码的一个特定位置,并告诉您究竟该怎么做。

that said, one typical way to do this is to use regular links (or react-router Links ) inside your table instead of Buttons, where each link includes an id parameter: 也就是说,一种典型的实现方法是在表内使用常规链接(或react-router Links )而不是Buttons,其中每个链接都包含一个id参数:

/newpage/<id>

. You then declare a react-router route which maps route to component: 然后,您声明一个将路由器映射到组件的react-router路由:

<Route path="newpage" component={NewPage}>
  <Route path=":classInfoId" component={NewPage} />
</Route>

. When the user visits, for example, /newpage/123 in your app then the NewPage component will be rendered. 当用户访问您的应用程序中的/newpage/123 ,将呈现NewPage组件。 React-router will inject a params prop that looks like: React-router将注入一个params属性,如下所示:

{classInfoId: 123}

. You then fetch from your Redux store the class info object with an id of 123 , and display it as you wish. 然后,您从Redux存储中获取ID为123的类信息对象,并根据需要显示它。

Use redux reducers and actions. 使用redux减速器和动作。

On click event, reduce your state to store id or classTitle. 发生click事件时,请降低状态以存储id或classTitle。 I guess you are rendering the table dynamically with the data you have. 我猜您正在使用拥有的数据动态呈现表。

onClick = (id) => () => {
  this.props.setCurrentCell(id) // setCurrentCell is your action
  this.props.push('/new-page') // push is react-router-redux action. These actions are mapped into props with mapDispatchToProps
}

// td rendering
<td><button onClick={this.onClick(cell.id)}>{cell.classTitle}<td>

Create action for setCurrentCell to store current cell id, and inject that with mapStatesToProps into the new table. 为setCurrentCell创建操作以存储当前单元格ID,并将其与mapStatesToProps一起注入到新表中。

If I'm understanding your question correctly... I'd suggest you look into the 'push' action that is part of react-router-redux. 如果我正确理解了您的问题...建议您研究一下react-router-redux中的“ push”操作。

link: https://github.com/reactjs/react-router-redux 链接: https//github.com/reactjs/react-router-redux

Do a search for 'push' and look for the example where they showcase it being used as below: 搜索“ push”并查找示例,以展示其用法,如下所示:

store.dispatch(push('/foo')) store.dispatch(推( '/富'))

What they are doing there is directing the user from their existing page to the next page '/foo' without losing their state. 他们在做什么,将用户从他们现有的页面定向到下一个页面“ / foo”,而不会丢失其状态。

Because your state is maintained, once you are on your next page, you can peek into your Redux store and all of your store data should still be there. 由于状态保持不变,因此在进入下一页后,您可以浏览Redux存储,并且所有存储数据仍应存在。

Hope this helps. 希望这可以帮助。

You should create the dispatcher to store the object into redux state container before redirecting to next page. 您应创建分派器,以将对象存储到redux状态容器中,然后再重定向到下一页。

Below is the code snippet. 下面是代码片段。

<Button disabled={!this.state.selectedParent} onClick={this.goNextPage} />

goNextPage() {
    const {dispatch} = this.props;
    dispatch(saveSelectedOrganization({
        parentNode:this.state.selectedParent,
        organizationData: this.props.list
    }));

    browserHistory.push('/admin/orgatnizations/create');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM