javascript - Reactjs - 将列表项移动到另一个列表

标签 javascript reactjs

我正在慢慢掌握(我认为) react 的诀窍。我想要做的是在一个组件中显示一个列表,然后使每个列表项(第 1 项、第 2 项等)都可以单击,以便在单击时可以在两个 <ItemList /> 组件之间来回移动.下面是我所拥有的,我认为我的问题是在 handleEvent() 的第一个列表中设置状态。

class SingleItem extends React.Component {
  render() {
    let data = this.props.data;

    return (
        <li>
            <div> {data.name} </div>
        </li>
    );
  }
}

class ItemList extends React.Component {
   render() {
     let itemArr = this.props.items;

     let listItems = itemArr.map((itemObj) => {
        return <SingleItem key={itemObj.id} data={itemObj}/>;
});

    return (
        <ul onClick={props.handleEvent}>
            {listItems}
        </ul>
    );
  }
}

class App extends React.Component {
constructor(props) {
    super(props);

    this.state = {
        boxOne: {listItems},
        boxTwo:''
    };

    this.handleEvent = this.handleEvent.bind(this);
}

handleEvent() {
    this.setState({
        boxOne: this.state.boxTwo,
        boxTwo: this.state.boxOne
    });
}
render() {
    return (
        <div>
            <ItemList items={this.state.boxOne} />
            <ItemList items={this.state.boxTwo} />
        </div>
    );
  }
};

var items = [
  {name: "Item 1", id: 1},
  {name: "Item 2", id: 2},
  {name: "Item 3", id: 3},
  {name: "Item 4", id: 4},
]

ReactDOM.render(<App />, document.getElementById('root'));

最佳答案

这是可以做到的..

所以,我将整个项目数组传递给所有 ItemList,但我也传递了状态,它只是一个数组,其中包含该列表中包含的项目的 ID,查看下面的逻辑并尝试理解,它是其实很简单..

class SingleItem extends React.Component {
  render() {
    let data = this.props.data;

    return (
        <li onClick={this.props.onClick}>
            <div> {data.name} </div>
        </li>
    );
  }
}

class ItemList extends React.Component {
   render() {
     let itemArr = this.props.allItems;
     let myItems = this.props.items;
     let handleEvent = this.props.handleEvent;

     let listItems = itemArr.map((itemObj) => {
        if (!myItems.includes(itemObj.id)) return null;

        return <SingleItem 
          key={itemObj.id}
          data={itemObj}
          onClick={() => handleEvent(itemObj.id)}
        />;
     });

     return (
        <ul>
            {listItems}
        </ul>
     );
   }
}

class App extends React.Component {
constructor(props) {
    super(props);

    this.state = {
        boxOne: props.items.map(item => item.id), // init the boxes with itemIds
        boxTwo: []
    };

    this.handleEvent = this.handleEvent.bind(this);
}

handleEvent(itemId) {
    const isInBoxOne = this.state.boxOne.includes(itemId);

    // Heres the magic, if the item is in the first Box, filter it out,
    // and put into the second, otherwise the other way around..
    this.setState({
        boxOne: isInBoxOne
          ? this.state.boxOne.filter(i => i !== itemId)
          : [ ...this.state.boxOne, itemId ]
        boxTwo: isInBoxOne
          ? [ ...this.state.boxTwo, itemId ]
          : this.state.boxTwo.filter(i => i !== itemId)
    });
}
render() {
    return (
        <div>
            <ItemList handleEvent={this.handleEvent} items={this.state.boxOne} allItems={this.props.items} />
            <ItemList handleEvent={this.handleEvent} items={this.state.boxTwo} allItems={this.props.items} />
        </div>
    );
  }
};

var items = [
  {name: "Item 1", id: 1},
  {name: "Item 2", id: 2},
  {name: "Item 3", id: 3},
  {name: "Item 4", id: 4},
]

ReactDOM.render(
  // Pass the initial items to your App
  <App items={items} />,
  document.getElementById('root')
);

关于javascript - Reactjs - 将列表项移动到另一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44836599/

相关文章:

javascript - 为什么我在提交表单时收到 500 错误?

javascript - 如何在 THREE.js 中获取被点击的元素

javascript - Yeoman 使用 args 通过代码调用生成器

javascript - 检查 TestCafe 测试中的值

javascript - 选择组背景颜色标签

javascript - 虚拟 DOM 创建顺序。哪个节点先创建?

javascript - 如何使用javascript将字符串中的电话号码和时间分开?

string - 检查 JSX 中的空字符串

reactjs - 在React js中使用redux表单时出现字段错误

reactjs - 带有 React Typescript 的 Firebase 9 : How do I change the querySnapshot types?