我正在尝试将类组件转换为功能组件。但这对我来说有点困难。你可以帮帮我吗?
我尝试这样做,但是没有用:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';
function (
props.profiles
props.movies
props.users
) {
return (
<div>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">ReactND - Coding Practice</h1>
</header>
<h1>How Popular is Your Favorite Movie?</h1>
<MovieCardsList profiles={profiles} movies={movies} users={users} />
</div>
);
}
export default App;
这是代码的一部分:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';
class App extends Component {
render() {
return (
<div>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">ReactND - Coding Practice</h1>
</header>
<h1>How Popular is Your Favorite Movie?</h1>
<MovieCardsList profiles={profiles} movies={movies} users={users} />
</div>
);
}
}
export default App;
我接近解决方案了吗?任何帮助,我都感激不尽。预先感谢。
答案 0 :(得分:1)
更改最少:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';
function App(props) {
return (
<div>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">ReactND - Coding Practice</h1>
</header>
<h1>How Popular is Your Favorite Movie?</h1>
<MovieCardsList profiles={props.profiles} movies={props.movies} users={props.users} />
</div>
);
}
export default App;
您还可以使用{...props}
对象传播在MovieCardsList上设置它们,或使用对象解构({ profiles, movies, users })
来替换(props)
。我个人将如何做:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';
export default ({ profiles, movies, users }) => (
<div>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">ReactND - Coding Practice</h1>
</header>
<h1>How Popular is Your Favorite Movie?</h1>
<MovieCardsList profiles={profiles} movies={movies} users={users} />
</div>
);
答案 1 :(得分:0)
要么接受prop作为参数:
function App = (props) => {
cosnt {profiles, movies, users} = props
return <div/>
}
或立即对其进行解构
function App = ({profiles, movies, users}) => {
return <div/>
}
完整代码:
import React from 'react';
import logo from './logo.svg';
import './App.css';
import MovieCardsList from './MovieCardsList';
function App = ({profiles, movies, users}) => {
return (
<div>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">ReactND - Coding Practice</h1>
</header>
<h1>How Popular is Your Favorite Movie?</h1>
<MovieCardsList profiles={profiles} movies={movies} users={users} />
</div>
);
}
导出默认应用;