【发布时间】:2018-09-11 15:27:34
【问题描述】:
所以,我仍然是 React 的初学者,我正在尝试保存“数量”的状态值
constructor(props){
super(props);
this.state = {
visible: false,
selected: false,
quantity: ""
}
this.toggleMenu = this.toggleMenu.bind(this);
}
我想将此数据传递给链接到该组件的父级的组件,并防止我返回时丢失数据。
<Link to={`/itemSelection/${sessionStorage.getItem("")}/checkout`}>PROCEED</Link>
______________________________________________________-
已编辑
所以父组件是里面的 ItemSelection 我导入 Item 组件并映射我从 api 获得的一些数据。
<div className="row">
{this.state.items.map(i => <Item name={i.name} quantity={i.quantity} />)}
</div>
我传递给 Item 的数量是我从 api 获得的全部数量。
Item 组件内部
constructor(props){
super(props);
this.state = {
visible: false,
selected: false,
quantity: ""
}
this.toggleMenu = this.toggleMenu.bind(this);
}
这里的数量是我为我选择的每件商品选择的数量。当我单击结帐组件的链接时,我丢失了这些数据,如果我返回 itemSelection 组件,我发现这些数据也丢失了。因此,我想将每个项目的这个数量传递给结帐组件,并在我再次返回 itemSelection 组件时防止丢失数据。希望这个澄清是有意义的。
【问题讨论】:
-
也许将数据存储在 cookie 中...npmjs.com/package/react-cookie
标签: reactjs ecmascript-6 react-props