下面的代码来自React Native tutorial
因为我是JS菜鸟,所以我很好奇:
1. 为什么
(text) => this.setState({text})
可以将输入值文本分配给对象的关键文本this.state
?
我很熟悉用 (text) => this.setState({'text':text})
来写它
2. 类型为{key}
与 {key:value}
相同?它们都是对象吗?
class PizzaTranslator extends Component {
constructor(props) {
super(props);
this.state = {text: ''};
}
render() {
return (
<View style={{padding: 10}}>
<TextInput
style={{height: 40}}
placeholder="Type here to translate!"
onChangeText={(text) => this.setState({text})}
/>
<Text style={{padding: 10, fontSize: 42}}>
{this.state.text.split(' ').map((word) => word && '🍕').join(' ')}
</Text>
</View>
);
}
}
最佳答案
实际答案只是我想说的:
{key} === {key:key}
由于传入对象的单个变量被视为键:值,两者是相同的。所以这会是这样的:
{"key":"key"}
关于javascript - React Native ECMAScript 6 对象语法只有键,没有显式值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42640917/