React[004] Component+数据传递
src/components/Header/index.js
1 | import React from 'react' |
src/components/index.js
1 | export {default as Header} from './Header' |
src/App.js 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import React, { Component } from 'react'
import {
Header
} from './components'
export default class App extends Component {
render() {
return (
<>
<h1>app</h1>
<Header />
</>
)
}
}
src/index.js
1 | import React from 'react' |
数据传递
App.js
1 | import React, { Component } from 'react' |
components/TodoHeader/index.js 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import React from 'react'
import PropTypes from 'prop-types'
export default function TodoHeader(props) {
console.log(props)
return (
<>
<h1>
{props.children}
</h1>
<h3>
{props.desc}
</h3>
<p> {props.x + props.y} </p>
</>
)
}
TodoHeader.propTypes = {
desc: PropTypes.string,
x: PropTypes.number,
y: PropTypes.number,
z: PropTypes.number.isRequired,
children: PropTypes.string.isRequired
}