React-Native:访问 TextInput 中的关键道具
React-Native: Access to key prop in TextInput
我尝试访问 TextInput 中的 key 道具以将其保存在状态中(然后在 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 的新手。你知道如何解决这个问题吗?
非常感谢! :)
为什么不这样做呢?
render() {
const { playerAmount } = this.props;
var textBoxes = [];
for (var i = 0; i < playerAmount; i++) {
var placeholderText = 'Player ' + (i + 1);
const key = i+1;
textBoxes.push(
<TextInput
key = {key}
onChangeText={(text) => {
const Player = Object.assign({}, this.state.Player, { playerName: text, playerNumber: key});
this.setState({ Player });
}}
placeholder={placeholderText}
placeholderTextColor="grey"
>
</TextInput>
);
}
}
class Firebase extends React.Component{
state={
name:'',
number:'',
}
onChangeTextInput = key => val => {
this.setState({[key]:val})
}
render(){
return(
<View>
<TextInput
value={this.state.name}
onChangeText={this.onChangeTextInput('name')}
/>
<TextInput
value={this.state.number}
onChangeText={this.onChangeTextInput('number')}
/>
</View>
)
}
}
我尝试访问 TextInput 中的 key 道具以将其保存在状态中(然后在 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 的新手。你知道如何解决这个问题吗?
非常感谢! :)
为什么不这样做呢?
render() {
const { playerAmount } = this.props;
var textBoxes = [];
for (var i = 0; i < playerAmount; i++) {
var placeholderText = 'Player ' + (i + 1);
const key = i+1;
textBoxes.push(
<TextInput
key = {key}
onChangeText={(text) => {
const Player = Object.assign({}, this.state.Player, { playerName: text, playerNumber: key});
this.setState({ Player });
}}
placeholder={placeholderText}
placeholderTextColor="grey"
>
</TextInput>
);
}
}
class Firebase extends React.Component{
state={
name:'',
number:'',
}
onChangeTextInput = key => val => {
this.setState({[key]:val})
}
render(){
return(
<View>
<TextInput
value={this.state.name}
onChangeText={this.onChangeTextInput('name')}
/>
<TextInput
value={this.state.number}
onChangeText={this.onChangeTextInput('number')}
/>
</View>
)
}
}