gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:13:10 24 4
gpt4 key购买 nike

我正在慢慢掌握(我认为) 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com