【发布时间】:2020-07-28 20:48:34
【问题描述】:
我尝试访问 TextInput 中的 key 属性以将其保存在 state 中(然后在 Redux 中)。我在一个数组中创建了很多 TextInput 字段,就像我从第一个屏幕获得的一样:
render() {
const { playerAmount } = this.props;
var textBoxes = [];
for (var i = 0; i < playerAmount; i++) {
var placeholderText = 'Player ' + (i + 1);
textBoxes.push(
<TextInput
key = {i+1}
onChangeText={(text) => {
const Player = Object.assign({}, this.state.Player, { playerName: text, playerNumber: this.props.key});
this.setState({ Player });
}}
placeholder={placeholderText}
placeholderTextColor="grey"
>
</TextInput>
);
现在我尝试使用 key 属性设置 playerNumber 的状态。我试过key / {key} / this.props.key
构造函数:
constructor(props) {
super(props);
this.state =
{
Player:
{
playerName: "",
playerNumber: 0
}
}
}
如您所见,我对 React-Native 还是很陌生。你知道如何解决这个问题吗?
非常感谢! :)
【问题讨论】:
标签: react-native key textinput