[英]Parameter unreachable - [ts] cannot find name 'product'
Im trying to add my parameter product
to my Button-component我试图将我的参数
product
添加到我的按钮组件
My function handleIncrement
has product
set as follow:我的函数
handleIncrement
product
设置如下:
handleIncrement = product => {
console.log(product);
this.setState({ count: this.state.count + 1 });
};
My onClick event: onClick={() => this.handleIncrement(product)}
我的 onClick 事件:
onClick={() => this.handleIncrement(product)}
And from my understanding should make it reachable in my render()
but Typescript is giving me "Cannot find name 'Product'"根据我的理解,它应该可以在我的
render()
访问,但是 Typescript 给了我“找不到名称‘产品’”
Im still at a learning phase for both React and Typescript.我仍然处于 React 和 Typescript 的学习阶段。 Am I doing something wrong or have I got it all wrong?
我做错了什么还是我完全错了?
This is the complete code:这是完整的代码:
class Counter extends Component {
state = {
count: 0,
tags: ["tag1", "tag2", "tag3"]
};
renderTags() {
if (this.state.tags.length === 0) return <p>No tags!</p>;
return (
<ul>
{this.state.tags.map(tag => (
<li key={tag}>{tag}</li>
))}
</ul>
);
}
handleIncrement = product => {
console.log(product);
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<div>
<span className={this.getBadgeClasses()}>{this.formatCount()}</span>
<button
onClick={() => this.handleIncrement(product)}
className="btn btn-secondary btn-sm"
>
Increment
</button>
{(this.state.tags.length === 0 && "Please create new tag!") ||
"tags exists"}
{this.renderTags()}
</div>
);
}
private getBadgeClasses() {
let classes = "badge m-2 badge-";
classes += this.state.count === 0 ? "warning" : "primary";
return classes;
}
formatCount() {
const { count } = this.state;
return count === 0 ? "Zero" : count;
}
}
export default Counter;
In JS, variables can be scoped globally, within a function or within a block, depending on how or where they are declared.在 JS 中,变量可以在全局范围内、在函数内或在块内,这取决于它们的声明方式或位置。
var globalVar = 1; function example() { var functionVar = 2; if (functionVar) { let blockVar = 3; // let makes a variable "block-scoped" } }
Within any particular scope, you have access to variables defined in that scope but also all scopes above it, up to the global scope (global variables are visible everywhere).在任何特定范围内,您都可以访问在该范围内定义的变量以及它之上的所有范围,直到全局范围(全局变量在任何地方都可见)。
var globalVar = 1; function example() { // here we also have access to globalVar, since it's in an upper scope var functionVar = 2; if (functionVar) { // here we have access to globalVar and functionVar, since they are both in an upper scope let blockVar = 3; } }
In the render
method of your component, you are using a variable named product
but you are not defining it anywhere within the scope of render
(or in a scope above):在组件的
render
方法中,您使用了一个名为product
的变量,但没有在render
范围内(或以上范围内)的任何地方定义它:
render() {
return (
<div>
{/* ... */}
<button
{/* ... */}
onClick={() => this.handleIncrement(product)}
{/* ^^^^^^^ not defined anywhere in this scope */}
>
{/* ... */}
</div>
);
}
Here's an example of how that breaks:这是一个如何打破的例子:
function render() { console.log(product); } render();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.