【发布时间】:2019-07-20 12:44:14
【问题描述】:
constructor(props){
super(props)
let Path = window.location.href.split('/')
let name = Path[Path.length-1]
name = name.replace(/%20/g, ' ')
const auxKeys = Object.keys(this.props.test.aux)
let aux = {}
auxKeys.map((value, key) => this.props.test.aux[value].name === name ?
aux = {
val0: value,
const {val1} = this.props.test.aux[value], //i need to do this, but doesn't work
val2: this.props.test.aux[value].val2, //when i do this, i pass the reference of props, and when i change the state, i change the props... but if user cancel this operation, the props was changed yet
val3: this.props.test.aux[value].val3,
val4: this.props.test.aux[value].val4,
val5: this.props.test.aux[value].val5
}:
null
)
const {val0, val1, val2, val3, val4, val5} = aux
this.state = {
aux: {
val0,
val1,
val2,
val3,
val4,
val5,
}
}
}
handleChange = field => event => {
const aux = {
...this.state.aux,
[field]: event.target.value
}
this.setState({aux}) //when i do this, i don't want to change the props, i want just change this.state.aux.'field'
}
我想使用解构赋值映射函数...我怎样才能正确地完成这项工作?我需要复制道具并且不要参考...我需要更改状态,而不是道具
【问题讨论】:
-
const {val1} = this.props.test.aux[value],这不是扩展运算符,更像是对象解构。 -
你为什么在这里使用
map?你对结果数组什么都不做。为什么要在三元表达式中分配给aux? -
对不起,我没有得到你想要的。这种无效的语法根本不清楚。输入 (
this.props.test.aux) 是什么,预期输出是什么?解构、传播和map方法有什么关系? -
@Bergi 我知道你是对的:扩展语法不是运算符,但在我的母语中,MDN 被称为
spread operator:El operador de propagación spread operator permite que una expresión sea expandida en situaciones donde se esperan múltiples argumentos (llamadas a funciones) o múltiples elementos (arrays literales)。 也许相同的翻译适用于其他语言,这会产生混淆。
标签: javascript reactjs