- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个网络应用程序,人们可以点击鸡尾酒并更改配方以增加或减少每种成分的含量。为此,我首先从 API 端点加载饮料数据库,然后为数据库中的每种饮料创建一个子项,并将属性“drink”设置为定义饮料名称和配方的 JSON 对象。
每个 child 都有一个按钮来弹出一个模式,该模式有一个按钮组来调整每种成分的数量。我这样做的方法是将饮料配方作为属性传递给模式,然后将该配方从 Prop 复制到状态。
然后,当有人调整饮料配方时(他们可以为每种成分选择“低”、“常规”或“高”),我在模态上调用一个类函数,该函数传递配方列表中成分的索引,并乘以原始配方的常数。
在函数中,我设置了一个等于 props.recipe 的临时配方,然后将该因子乘以成分的原始数量。然后我通过调用 this.State 将已被因子修改的临时配方复制到状态
但是正在发生的事情是它似乎正在改变 props.recipe 以及 state.recipe。
为什么会发生这种情况?我不认为 Prop 是允许改变的。
其次,有没有更好的方法呢?我是新手,这是我的第一个网络应用程序。在最简单的形式中,我只是试图限制每种成分可以减少/增加的量,但仍然为最终用户提供更改它的选项。
Here is a screenshot with the console open所以你可以看到“之前”和“之后”如何改变,即使原始数量是3,它都是6。我点击“高”按钮,使成分的原始数量加倍,但如果我点击它它再次加倍,而不是停留在 6(这是 props 中存储的原始成分量的 2 倍)。
这是模式的代码:
import React from 'react';
import ReactDOM from 'react-dom';
import {Modal, ButtonGroup, Button, Table} from 'react-bootstrap';
export default class DrinkInfo extends React.Component {
constructor(props){
super(props);
this.state = {
recipe: props.drink.recipe,
};
console.log('Drink Info Initial Recipe is');
console.log(this.props);
}
adjustAmount(ingredient, level){
console.log('before change')
var tempRecipe = this.props.drink.recipe;
console.log(tempRecipe)
tempRecipe[ingredient].amount = level * tempRecipe[ingredient].amount;
//tempRecipe[ingredient].amount = level * this.props.drink.recipe[ingredient].amount;
console.log('after change')
console.log(tempRecipe)
this.setState({ recipe: tempRecipe});
}
render(){
const drinkName = this.props.drink.drink;
//console.log(this.state.recipe)
const recipeTableBody = this.state.recipe.map((di, value) => {
//console.log(di.ingredient);
return(
<tr key = {di.ingredient}>
<td>{value}</td>
<td>{di.ingredient}</td>
<td>{di.amount}</td>
<td>
<ButtonGroup>
<Button onClick={() => this.adjustAmount(value, 0.5)}> Low </Button>
<Button onClick={() => this.adjustAmount(value, 1)}> Regular </Button>
<Button onClick={() => this.adjustAmount(value, 2)}> High </Button>
</ButtonGroup>
</td>
</tr>
);
});
//{console.log('Creating Info Modal for: ' + this.props.drink.drink)}
const recipeTable = (
<Table striped bordered condensed hover>
<thead>
<tr>
<th>#</th>
<th>Ingredient</th>
<th>Amount</th>
<th>Adjust</th>
</tr>
</thead>
<tbody>
{recipeTableBody}
</tbody>
</Table>
)
return(
<div>
<Modal show={this.props.show} onHide={this.props.onHide} bsSize="large" aria-labelledby="contained-modal-title-sm">
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-sm"> {this.props.drink.drink} </Modal.Title>
</Modal.Header>
<Modal.Body>
<h4> {this.props.drink.drink} Recipe </h4>
{recipeTable}
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onHide}>Close</Button>
</Modal.Footer>
</Modal>
</div>
);
}
}
最佳答案
正如有人提到的,尽量让 future 的问题更短、更简洁。这样可以更轻松地提供帮助。
也就是说,我想我在您的 adjustAmount()
函数中发现了您的问题。
看一下:
adjustAmount(ingredient, level){
var tempRecipe = this.props.drink.recipe;
tempRecipe[ingredient].amount = level * tempRecipe[ingredient].amount;
this.setState({ recipe: tempRecipe});
}
您正在获取 this.props.drink.recipe
并将其设置为等于 tempRecipe
。由于recipe
是一个对象,这基本上在tempRecipe
处创建了对该对象的引用。换句话说,tempRecipe === 配方
。
这意味着,当您更改 tempRecipe
时,您也在更改 recipe
。虽然严格来说 React 不应该让你改变 props,但它没有任何东西可以卡住它们。 JavaScript 就是 JavaScript,所以它可以让你。
您应该做的是克隆 this.props.drink.recipe
,然后使用克隆。
adjustAmount(ingredient, level){
var tempRecipe = Object.assign({}, this.props.drink.recipe);
tempRecipe[ingredient].amount = level * tempRecipe[ingredient].amount;
this.setState({ recipe: tempRecipe});
}
这将assign()
来自recipe
的所有属性并将它们分配给一个新对象,从而有效地克隆它。
重要说明:Object.assign({}, someObj)
只会创建浅克隆。如果您的对象具有多个级别的深度(看起来确实如此),那么您应该获取或创建一个深度克隆对象,而不是 Object.assign()
。
只需获取其中一个,然后执行以下操作:
adjustAmount(ingredient, level){
var tempRecipe = deepClone(this.props.drink.recipe);
tempRecipe[ingredient].amount = level * tempRecipe[ingredient].amount;
this.setState({ recipe: tempRecipe});
}
它们有很多,所以我不会在这里重新实现它们。拥有它们的几个库是 lodash、underscore,或者 NPM 中提供了一些独立的库。
关于javascript - 改变状态会导致 props 改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44641691/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!