Попробуйте заменить метод addtoFoving на обычную функцию вместо функции стрелки, как эта
class RecipeList extends Component {
constructor() {
this.addtoFavorites = this.addtoFavorites.bind(this);
}
addtoFavorites (ev){
const val = ev.target.dataset.value;
this.props.recipeToFavorite(val);
console.log(val)
}
renderRecipe(recipeData) {
let recipeName = recipeData.recipeName;
let recipeId = recipeData.id;
let recipeIngredients = recipeData.ingredients.join(", ");
let recipeURL = "https://www.yummly.com/recipe/" + recipeData.id;
let recipeImage = recipeData.smallImageUrls;
var recipeDataObj = { name:recipeName, recipeID:recipeId, recipeImage:recipeImage, recipeURL: recipeURL }
}
return (
<div>
<div key={recipeData.id}>
<div>
<img
src= {recipeImage}
alt="FoodPic"
/>
<h4> {recipeName} </h4>
<div>
<h3>Ingredients</h3>
</div>
<ul>
{recipeIngredients}
</ul>
<h6>
<a href={recipeURL}>
Recipe
</a>
</h6>
</div>
</div>
<button
onClick={this.addtoFavorites}
data-value={recipeDataObj}
>
Fav
</button>
</div>
);
}
render() {
return (
<div>
<h2 className="">Recipes</h2>
<div className="">{this.props.recipes.map(this.renderRecipe)}</div>
</div>
);
}
}
function mapStateToProps({ recipes }) {
//console.log("List Recipes", recipes)
return {
recipes
};
}
export default connect(mapStateToProps)(RecipeList);
Нет, нет никакого броска, вовлеченного с тех пор List<T>
IS-A
IEnumerable<T>
. Это использует полиморфизм, который не требует кастинга.
Править: Вот пример:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
foo(new List<int>());
}
static void foo(IEnumerable<int> list) { }
}
IL для Main
:
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 8
L_0000: nop
L_0001: newobj instance void [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
L_0006: call void Program::foo(class [mscorlib]System.Collections.Generic.IEnumerable`1<int32>)
L_000b: nop
L_000c: ret
}
И поскольку Вы видите, что нет никакого включенного кастинга. Экземпляр List<T>
продвинут на стек и их foo
сразу назван после.
Я не полагаю, что существует стоимость. Так как типы уже реализуют IEnumerable, объект должен смочь использоваться немедленно (обратите внимание, что это - главным образом предположение; я понятия не имею о том, как vtables CLR действительно выполняют незаметную работу).
Если будет стоимость, то это будет так удивительно маленьким, что, если это имеет значение, Вы, вероятно, не должны использовать CLR для начала.