Reactjs react组件的异步呈现,如何设置全局加载组件?
我只希望在异步调用成功后渲染组件。在此之前,我想展示一个加载组件来改善用户体验 在一个组件中,我知道如何处理它,指的是。但在我的项目中,有许多页面(组件)显示带有异步调用的信息Reactjs react组件的异步呈现,如何设置全局加载组件?,reactjs,redux,react-router,Reactjs,Redux,React Router,我只希望在异步调用成功后渲染组件。在此之前,我想展示一个加载组件来改善用户体验 在一个组件中,我知道如何处理它,指的是。但在我的项目中,有许多页面(组件)显示带有异步调用的信息 那么,有没有一种方法可以在某个位置设置全局加载组件,而不是在每个组件中写入内部状态?可以查看react中的HOC模式: 你可以这样做: function withAsynchronous(WrappedComponent, url) { return class extends React.Component {
那么,有没有一种方法可以在某个位置设置全局加载组件,而不是在每个组件中写入内部状态?可以查看react中的HOC模式: 你可以这样做:
function withAsynchronous(WrappedComponent, url) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true
};
}
componentDidMount() {
// You async call
GetAsync(url, data => {
this.setState({ isLoading: false, data: data });
});
}
render() {
if (this.state.isLoading) {
return (<YourLoadingComponent />);
}
return <WrappedComponent data={this.state.data} {...this.props} />;
}
};
}
componentDidMount() {
const YourAsyncPage = withAsynchronous(
YourPage,
'/your/page/url'
);
}
render() {
return (<YourAsyncPage />);
}
异步函数(WrappedComponent,url){
返回类扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
孤岛加载:正确
};
}
componentDidMount(){
//您可以使用异步调用
GetAsync(url,数据=>{
this.setState({isLoading:false,data:data});
});
}
render(){
if(此.state.isLoading){
返回();
}
返回;
}
};
}
然后像这样渲染:
function withAsynchronous(WrappedComponent, url) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true
};
}
componentDidMount() {
// You async call
GetAsync(url, data => {
this.setState({ isLoading: false, data: data });
});
}
render() {
if (this.state.isLoading) {
return (<YourLoadingComponent />);
}
return <WrappedComponent data={this.state.data} {...this.props} />;
}
};
}
componentDidMount() {
const YourAsyncPage = withAsynchronous(
YourPage,
'/your/page/url'
);
}
render() {
return (<YourAsyncPage />);
}
componentDidMount(){
const yoursynchpage=withAsynchronous(
你的页面,
“/your/page/url”
);
}
render(){
返回();
}
也许可以看看react中的HOC模式:
你可以这样做:
function withAsynchronous(WrappedComponent, url) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true
};
}
componentDidMount() {
// You async call
GetAsync(url, data => {
this.setState({ isLoading: false, data: data });
});
}
render() {
if (this.state.isLoading) {
return (<YourLoadingComponent />);
}
return <WrappedComponent data={this.state.data} {...this.props} />;
}
};
}
componentDidMount() {
const YourAsyncPage = withAsynchronous(
YourPage,
'/your/page/url'
);
}
render() {
return (<YourAsyncPage />);
}
异步函数(WrappedComponent,url){
返回类扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
孤岛加载:正确
};
}
componentDidMount(){
//您可以使用异步调用
GetAsync(url,数据=>{
this.setState({isLoading:false,data:data});
});
}
render(){
if(此.state.isLoading){
返回();
}
返回;
}
};
}
然后像这样渲染:
function withAsynchronous(WrappedComponent, url) {
return class extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true
};
}
componentDidMount() {
// You async call
GetAsync(url, data => {
this.setState({ isLoading: false, data: data });
});
}
render() {
if (this.state.isLoading) {
return (<YourLoadingComponent />);
}
return <WrappedComponent data={this.state.data} {...this.props} />;
}
};
}
componentDidMount() {
const YourAsyncPage = withAsynchronous(
YourPage,
'/your/page/url'
);
}
render() {
return (<YourAsyncPage />);
}
componentDidMount(){
const yoursynchpage=withAsynchronous(
你的页面,
“/your/page/url”
);
}
render(){
返回();
}
您需要一种将状态的正确部分委托给组件的方法,因此如果您的应用程序有一个登录组件,那么应该向登录组件中的处理程序传递它负责的状态部分
如果组件的加载看起来不同,则可以在组件状态下使用加载成员,并使用全局实用程序模块中的函数将其设置为true或false,或从应用程序中注入该函数
应用程序的处理程序(操作类型为数组):
登录处理程序
switch(action.type.slice[0,1]) {
case "toggleLoading":
return utils.toggleLoading(state)
}
由于组件不知道其嵌套方式,因此必须将动作类型数组从应用程序传递到组件,并从组件传递到子组件(动作类型可以添加到状态)
如果您想使用一个组件来显示和隐藏加载,那么代码与此类似,只是您的登录组件将向加载组件委托toggleLoading操作,其方式与应用程序将其委托给登录组件的方式相同
在这种情况下,无需传递实用程序对象,因为只有一个加载组件,因此不会重复加载设置的实现。您需要一种方法将状态的正确部分委托给该组件,因此,如果应用程序有一个登录组件,那么应该向登录组件中的处理程序传递它负责的状态部分
如果组件的加载看起来不同,则可以在组件状态下使用加载成员,并使用全局实用程序模块中的函数将其设置为true或false,或从应用程序中注入该函数
应用程序的处理程序(操作类型为数组):
登录处理程序
switch(action.type.slice[0,1]) {
case "toggleLoading":
return utils.toggleLoading(state)
}
由于组件不知道其嵌套方式,因此必须将动作类型数组从应用程序传递到组件,并从组件传递到子组件(动作类型可以添加到状态)
如果您想使用一个组件来显示和隐藏加载,那么代码与此类似,只是您的登录组件将向加载组件委托toggleLoading操作,其方式与应用程序将其委托给登录组件的方式相同
在这种情况下,不需要传递实用程序对象,因为只有一个加载组件,所以不会重复加载设置的实现。在每个API启动之前打开
DISPATCH
用于API\u START
的操作
dispatch(apiStart());
const apiStart = ()=>{
type : "API_START"
}
完成API调用后,
为API\u FINSIH
分派操作
dispatch(apiFinish());
const apiFinish = ()=>{
type : "API_FINSIH"
}
减速器:
const uiReducer = (state, action) => {
switch (action.type) {
case "API_START":
return Object.assign({}, state, {
requestsCOunt: state.requestsCOunt + 1
});
case "API_FINSIH":
return Object.assign({}, state, {
requestsCOunt: state.requestsCOunt - 1
});
}
}
在状态属性的组件检查中state.requestsCOunt
如果在每次API启动之前都启用了state.requestScont
DISPATCH
用于API\u START
的操作
dispatch(apiStart());
const apiStart = ()=>{
type : "API_START"
}
完成API调用后,
为API\u FINSIH
分派操作
dispatch(apiFinish());
const apiFinish = ()=>{
type : "API_FINSIH"
}
减速器:
const uiReducer = (state, action) => {
switch (action.type) {
case "API_START":
return Object.assign({}, state, {
requestsCOunt: state.requestsCOunt + 1
});
case "API_FINSIH":
return Object.assign({}, state, {
requestsCOunt: state.requestsCOunt - 1
});
}
}
在状态属性的组件检查中state.requestsCOunt
如果是state.requestsCOunt,则使用加载组件异步呈现组件的基本示例如下:
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import YourLoadingComponent from './YourLoadingComponent.react.js';
export default class YourComponent extends React.PureComponent {
constructor(props){
super(props);
this.state = {
data: null
}
}
componentDidMount(){
const data = {
optPost: 'userToStat01',
message: 'We make a research of fetch'
};
const endpoint = 'http://example.com/api/phpGetPost.php';
const setState = this.setState.bind(this);
fetch(endpoint, {
method: 'POST',
body: JSON.stringify(data)
})
.then((resp) => resp.json())
.then(function(response) {
setState({data: response.message});
});
}
render(){
return (<div>
{this.state.data === null ?
<YourLoadingComponent />
:
<div>{this.state.data}</div>
}
</div>);
}
}
从“React”导入React;
从“react dom”导入react dom;
从“道具类型”导入道具类型;
从“/YourLoadingComponent.react.js”导入YourLoadingComponent;
导出默认类YourComponent扩展React.PureComponent{
建造师(道具){
超级(道具);
此.state={
数据:空
}
}
componentDidMount(){
常数数据={
optPost:'userToStat01',
消息:“我们正在对fetch进行研究”
};
常数端点http://example.com/api/phpGetPost.php';
const setState=this.setState.bind(this);
获取(端点{
方法:“POST”,
正文:JSON.stringify(数据)
})
.然后((resp)=>resp.json())
.然后(功能(响应){
setState({data:response.message});
});
}
撕裂