
上QQ阅读APP看书,第一时间看更新
Post component
Let's start by writing the Post component as follows:
Create a new src/components/ folder, and in this folder, create a Post.jsx file.
Remember, presentational components are concerned with how things look. The Post component will take a user and text as properties and display a username in bold next to the text. Put the following code in the src/components/Post.jsx file:
import React from 'react' const Post = ({ user, text }) => <span><b>{user}</b> - {text}</span> export default Post
In files that use JSX, it is important to import React at the beginning of the file. This is needed because JSX gets translated to calls to React.createElement.
In the src/index.js file, we can now import and render the Post component with ReactDOM. Replace the contents of the file with the following code:
import React from 'react' import ReactDOM from 'react-dom' import Post from './components/Post.jsx' ReactDOM.render( <Post user="dan" text="hello world!" />, document.getElementById('root') )
Our application should now look as follows:

Rendering a single Post component