我有一个普通的选择器,它只是用来获得状态的一部分:
export const getAllPosts = state => {
return state.posts;
};
Run Code Online (Sandbox Code Playgroud)
如果我使用重新选择来包装选择器:
import { createSelector } from 'reselect';
export const allPosts = createSelector(
getAllPosts,
(posts) => posts
);
Run Code Online (Sandbox Code Playgroud)
它是否有任何意义,如提高性能?在我看来,包装是不必要的.
这是我的主要代码?App组件连接到 Redux 的商店:
class App extends Component {
render() {
const { requestQuantity } = this.props;
return (
<div>
<Router>
<Switch>
<Route exact path="/" component={PostList} />
<Route path="/login" component={Login} />
<Route path="/topics" component={PostList} />
</Switch>
</Router>
{requestQuantity > 0 && <Loading />}
</div>
);
}
}
const mapStateToProps = (state, props) => {
return {
requestQuantity: getRequestQuantity(state)
};
};
export default connect(mapStateToProps)(App);
Run Code Online (Sandbox Code Playgroud)
PostList 组件也连接到 Redux 的商店:
class PostList extends Component {
componentDidMount() {
this.props.fetchAllPosts();
}
render() {
const …Run Code Online (Sandbox Code Playgroud) 我getServerSideProps 用来获取初始文章数据,如下所示:
export const getServerSideProps = async () => {\n const url =\n "https://conduit.productionready.io/api/articles?limit=10&offset=0";\n const res = await fetch(url);\n const resJson = await res.json();\n\n return {\n props: {\n data: resJson.articles\n }\n };\n};\nRun Code Online (Sandbox Code Playgroud)\n我需要在页面更改时更新文章\xef\xbc\x8cso我有以下代码:
\nexport default function IndexPage(props) {\n const { data } = props;\n const [articles, setArticles] = useState(data);\n const [page, setPage] = useState(0);\n\n useEffect(() => {\n const fetchData = async (page) => {\n const url = `https://conduit.productionready.io/api/articles?limit=10&offset=${page}`;\n const res = await fetch(url);\n const resJson = …Run Code Online (Sandbox Code Playgroud)