【发布时间】:2019-10-09 15:01:52
【问题描述】:
state = {
persons:[
{id:"cbhc", name:"surya",age:26,sex:"male"},
{id:"rgt", name:"sachin",age:36,sex:"male"},
{id:"cbcchc", name:"rahul",age:46,sex:"male"}
],
showdetails:false,
**counter:0**,
};
以上是我的应用程序中的数据状态:
// Wrong
this.setState({
counter: this.state.counter + this.props.increment,
});
要修复它,请使用setState() 的第二种形式,它接受函数而不是对象。该函数将接收先前的状态作为第一个参数,并将应用更新时的道具作为第二个参数:
// Correct
this.setState((state, props) => ({
counter: state.counter + props.increment
}));
这里到底是什么:props.increment ????
我的一段代码:
this.setState((state, props) => ({
counter: state.counter + props.increment
}));
我想知道props.increment是什么??
我的组件骨架:
import React from "react";
//import mystyles from "./person.module.css";
//import Studentoutput from "./cockpit/cockpit";
const Singlestudent = props => {
console.log("child component skeleton rendering...");
return (
<div>
<p onClick={props.click}>{props.name}</p>
<p>{props.age}</p>
<p>{props.id}</p>
**<p>{props.increment}</p>**
<input type="text" onChange={props.update} value={props.name} />
</div>
);
};
export default Singlestudent;
由于我的状态数据嵌入在嵌套数组和对象中,因此使用 map 方法来构造我的骨架组合数据,如下所示:
// import React from "react";
// //import mystyles from "./person.module.css";
// const Studentoutput = props => <input type="text" value={props.name} />;
// export default Studentoutput;
import React from "react";
import Singlestudent from "./student/singlestudent";
const Studentinfo = props => {
console.log("child component details rendering...");
return props.details.map((studentinfo, index) => {
return (
<Singlestudent
key={studentinfo.id}
name={studentinfo.name}
age={studentinfo.age}
**increment={props.increment}**
update={props.updated(studentinfo.id)}
click={props.clicked(studentinfo.id)}
/>
);
});
};
export default Studentinfo;
我通过了 increment={1} ,硬编码它。
现在终于将上面的内容传递给在浏览器上呈现的我的主要父级
return (
<div className={mystyles.parent}>
<Studentinfo
details={this.state.details}
updated={this.updateStudentHandler}
clicked={this.deleteStudentHandler}
**increment={1}**
/>
</div>
);
从上面的代码 sn-p 我通过 updateStudentHandler 改变我的计数器值
updateStudentHandler = id => event => {
//debugger;
const studentIndex = this.state.details.findIndex(d => d.id === id);
console.log(studentIndex);
let duplicate = {
...this.state.details[studentIndex]
};
duplicate.name = event.target.value;
const dd = [...this.state.details];
dd[studentIndex] = duplicate;
this.setState({
details: dd
});
this.setState((referencetoprevState, Props) => {
return {
counter: referencetoprevState.counter + Props.increment
};
});
};
一旦我更改输入框中的文本,我的计数器应该更新但它返回 NaN,为什么 ???? 请参阅下面的屏幕截图
但是如果我改变下面的代码
this.setState((state, props) => {
return { counter: state.counter + props.increment };
});
使用 值 (9000) 而不是 props.increment 会按预期更新计数器值。
this.setState((state, props) => {
return { counter: state.counter + 9000 };
});
为什么我需要明确提供值,而不仅仅是像 props.increment 类似于 state.counter 因为 state.counter 将其值从前一个状态设为 0 但是props.increment 没有从 Studentinfo comp 的用户定义组件的 jsx 中的 increment={1} 中取值 1? 有什么限制/原因吗??
【问题讨论】:
-
Props 是传递给您的组件的内容:
<YourComponent increment={1} />,您不是要使用 this.state.increment 吗?
标签: javascript reactjs