Reactjs 如何在对象数组中添加对象?反应?

Reactjs 如何在对象数组中添加对象?反应?,reactjs,react-native,Reactjs,React Native,我的项目是,一个对象数组,在其中我只获得名称,并在屏幕上以3:3的形式呈现,使用按钮“下一步”和“上一步”更改名称,并可以过滤字母 我想添加一个新值,键入输入并单击按钮add 我的代码按钮: addItem=()=>{ 常数无效={ id:0, 名称:this.state.input } 这是我的国家({ 过滤:此.state.filtered.concat(无效), 当前页面:0 }) }看起来您已经在用键入的输入更新您的状态 因此,在“添加”按钮中,您可以获取状态值并将其推送到人员数组中。

我的项目是,一个对象数组,在其中我只获得名称,并在屏幕上以3:3的形式呈现,使用按钮“下一步”和“上一步”更改名称,并可以过滤字母

我想添加一个新值,键入输入并单击按钮
add

我的代码按钮:

addItem=()=>{
常数无效={
id:0,
名称:this.state.input
}
这是我的国家({
过滤:此.state.filtered.concat(无效),
当前页面:0
})

}
看起来您已经在用键入的输入更新您的状态

因此,在“添加”按钮中,您可以获取状态值并将其推送到
人员
数组中。大概是这样的:

addItem = () => {
  const { inputValue, people } = this.state;
  if (!inputValue) return; // check if inputValue has any value
  people.push({ id: people.length+1, name: inputValue )} // I don't recommend using sequencial ids like this, you'd better have a handler to generate it for you
  this.setState({ people, inputValue: '' }); // update people and clear input
}

希望它有帮助

您的状态中包含对象数组,然后使用
setState

this.state = {
      elementsPerPage:3,
      currentPage:0,
      peoples,
      input: "",
      filtered: peoples,
      teste: '',
      peoples: [
  {id:0, name:"Jean"}, 
  {id:1, name:"Jaha"}, 
  {id:2, name:"Rido"}, 
  {id:3, name:"Ja"}, 
  {id:4, name:"Letia"}, 
  {id:5, name:"Di"}, 
  {id:6, name:"Dane"}, 
  {id:7, name:"Tamy"}, 
  {id:8, name:"Tass"},
  {id:9, name:"Ts"}, 
  {id:10, name:"Abu"}, 
  {id:11, name:"Ab"}];
    };
要更新peoples数组,首先需要创建peoples数组的副本,修改副本,然后使用setState进行更新

let { peoples } = this.state;
peoples.push({ id:12, name:"Jean"}) 
this.setState({peoples: peoples})

您可以为您的阵列使用状态,并在eventsHello soutot上更新阵列,谢谢您的回答,但不起作用…:(可能是打字错误。我刚看到你的州道具是
人民
,在我的代码中我写了
人民
。请检查情况是否如此。如果不是,我建议在你的
添加项中添加
控制台.log(this.state)
,以了解从你的州检索到了什么。如果这有帮助,请告诉我。)