Created
November 20, 2016 04:42
-
-
Save ajcronk/c7adb6e501d17986ef98f19400da0f86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import FlipMove from 'react-flip-move'; | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.handeClick = this.handeClick.bind(this); | |
this.state = { | |
items:[ | |
{ id:"Item1", visible: true }, | |
{ id:"Item2", visible: false }, | |
{ id:"Item3", visible: false }, | |
] | |
} | |
} | |
handeClick() { | |
const items = this.state.items; | |
items[1].visible = true; | |
this.setState({items: items}); | |
} | |
render() { | |
return ( | |
<div> | |
<Button onClick={this.handeClick}/> | |
<FlipMove> | |
{/* works */} | |
{this.state.items.filter(item => item.visible).map(item => <Item key={item.id} item={item} />)} | |
{/* does not work */} | |
{/* {this.state.items.map(item => <Item key={item.id} item={item} />)} */} | |
</FlipMove> | |
</div> | |
); | |
} | |
} | |
class Item extends Component { | |
render() { | |
if (!this.props.item.visible) { | |
return null; | |
} | |
else { | |
return ( | |
<li>{this.props.item.id}</li> | |
); | |
} | |
} | |
} | |
class Button extends Component { | |
render() { | |
return ( | |
<button onClick={this.props.onClick}>Click</button> | |
); | |
} | |
} | |
export default App; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create-react-app flip-move-empty | |
cd flip-move-empty | |
npm install react-flip-move | |
npm start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment