Created
March 25, 2016 17:17
-
-
Save notakaos/aec845d24c118749ea20 to your computer and use it in GitHub Desktop.
Meteor 1.3 + React Simple Memo Step 5-2
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
// imports/ui/components/MemoItem.js | |
import React from 'react'; | |
export default class MemoItem extends React.Component { | |
constructor(props) { | |
super(props); | |
this.onClick = this.onClick.bind(this); | |
} | |
onClick(event) { | |
event.preventDefault(); | |
const { memo, removeMemo } = this.props; | |
removeMemo(memo._id); | |
} | |
render() { | |
const { memo } = this.props; | |
return ( | |
<div className="memo-item"> | |
<a href="#" onClick={this.onClick} className="remove-button"> | |
× | |
</a> | |
<textarea className="textarea" defaultValue={memo.content} /> | |
</div> | |
); | |
} | |
} | |
MemoItem.propTypes = { | |
memo: React.PropTypes.object.isRequired, | |
removeMemo: React.PropTypes.func.isRequired, | |
}; |
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
// imports/ui/components/MemoList.js | |
import React from 'react'; | |
import MemoItem from './MemoItem'; | |
export default class MemoList extends React.Component { | |
render() { | |
const { memos, removeMemo } = this.props; | |
return ( | |
<div className="memo-list"> | |
{memos.map(memo => ( | |
<MemoItem key={memo._id} memo={memo} removeMemo={removeMemo}/> | |
))} | |
</div> | |
); | |
} | |
} | |
MemoList.propTypes = { | |
memos: React.PropTypes.array.isRequired, | |
removeMemo: React.PropTypes.func.isRequired, | |
}; |
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
// imports/ui/containers/AppContainer.js | |
import AppLayout from '../layouts/AppLayout'; | |
import { Memos } from '../../api/memos/memos'; | |
import { createContainer } from 'meteor/react-meteor-data'; | |
const createMemo = content => { | |
Memos.insert({content}); | |
}; | |
const removeMemo = memoId => { | |
Memos.remove({_id: memoId}); | |
}; | |
export default createContainer(() => { | |
return { | |
memos: Memos.find().fetch(), | |
createMemo, | |
removeMemo, | |
}; | |
}, AppLayout); |
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
// imports/ui/layouts/AppLayout.js | |
import React from 'react'; | |
import Header from '../components/Header'; | |
import MemoList from '../components/MemoList'; | |
export default class AppLayout extends React.Component { | |
constructor(props) { | |
super(props); | |
this.onClick = this.onClick.bind(this); | |
} | |
onClick() { | |
const { memos, createMemo } = this.props; | |
createMemo(`New memo ${memos.length + 1}`); | |
} | |
render() { | |
const { memos, removeMemo } = this.props; | |
return ( | |
<div className="container"> | |
<Header /> | |
<button className="add-button" onClick={this.onClick}>Add</button> | |
<MemoList memos={memos} removeMemo={removeMemo} /> | |
</div> | |
); | |
} | |
} | |
AppLayout.propTypes = { | |
memos: React.PropTypes.array.isRequired, | |
createMemo: React.PropTypes.func.isRequired, | |
removeMemo: React.PropTypes.func.isRequired, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment