简体   繁体   English

setState 如何在索引中排列? react-native

[英]How setState to array in index? react-native

i have in the state an array:我在 state 中有一个数组:

 constructor() {
    super();
    this.state = {
        title: "",
        content: ["",""],
        note_number: "",
        fetch_notes: "",
        img: "",
    }
}

so here i set the value to the array:所以在这里我将值设置为数组:

    const {params} = this.props.navigation.state;
    this.setState({title: params.data.title});
    let array_notes = this.state.content.slice();
    array_notes[0] = params.data.content;
    this.setState({content: array_notes});
    this.setState({note_number: params.note_number});
    this.setState({fetch_notes: params.fetch_notes});

as you can see, i create a new variable with the slice method, this will create a copy of the original array and with the index zero, i tell him in that index store the content (string) the insert to content, the problem is here where i print the array in the index 1, that should be empty, but it gets the same value as the index zero, i think its bacause the method onchangetext, which is:如您所见,我使用 slice 方法创建了一个新变量,这将创建原始数组的副本,并且索引为零,我告诉他在该索引中存储内容(字符串)插入内容,问题是在这里,我在索引 1 中打印数组,该数组应该为空,但它的值与索引 0 相同,我认为它的原因是 onchangetext 方法,即:

  <View style = {this.styles.textinput_container}>
                <TextInput style = {this.styles.TextInput_title} placeholder = "Title" multiline = {true} maxLength = {80} value = {this.state.title} onChangeText = {title => this.setState({title: title})}></TextInput>
                <TextInput placeholder = "Content" multiline = {true} value = {this.state.content[0]} onChangeText = {text => this.setState(({content:[text,this.state.content[0]]}))}></TextInput>
                <Text>{this.state.content[1]}</Text>
            </View>

Onchangetext function: Onchangetext function:

text => this.setState(({content:[text,this.state.content[0]]}));

i dont really understad well this code, because a guy passed it to me, but it should only store in the position zero, why when im printing it我不太了解这段代码,因为一个人把它传给了我,但它应该只存储在 position 零中,为什么当我打印它时

<Text>{this.state.content[1]</Text>

the index 1 gets the same value as the zero?索引 1 获得与零相同的值?

Your function你的 function

text => this.setState(({ content: [text, this.state.content[0]] }));

is settings this.state.content to an array where the 0th index is equal to text and the 1st index is equal to this.state.content[0] ie whatever was in the 0th index previouslythis.state.content设置为一个数组,其中第 0 个索引等于text ,第一个索引等于this.state.content[0]即之前第 0 个索引中的任何内容

If you want the array to only have one value ie the 0th index you can do如果您希望数组只有一个值,即您可以执行的第 0 个索引

text => this.setState(({ content: [text]] }));

If you want text to replace the 0th index of this.state.content , you can do如果你想用text替换this.state.content的第 0 个索引,你可以这样做

text => this.setState(state => ({ content: [text, ...state.content.slice(1) ]]}));

EDIT:编辑:

If you want to update any arbitrary index, you could create a generic update function like so如果要更新任意索引,可以像这样创建通用更新 function

const setIndex = (vals, i, v) => [
  ...vals.slice(0, i),
  v,
  ...vals.slice(i+1)
];

and use it like this,并像这样使用它,

text => this.setState(state => ({ content: setIndex(state.content, 0, text) }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM