如何呈现React的缓存组件?

时间:2018-08-04 13:43:49

标签: reactjs redux react-redux

我正在使用React / Redux开发系统。

我想使用缓存的组件来响应客户请求。

window.CacheHolder = new Map()
export default class TestComponent extends Component {  
    constructor(props) {  
        super(props)  
    }  

    componentWillUnMount(){    
        // to cache this component.  
        window.CacheHolder.set('TestComponent', this);
    }  

    render() {  
        const cachedComponent = window.CacheHolder.get('TestComponent')  
        if (cachedComponent) {  
            return cachedComponent  
        }  
        return ...   
    }  
}  

当然,这种方式行不通。
有人知道其他方式吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用无状态组件,并将组件状态保留在redux存储中。然后,您可以将状态与localStorage redux-persist同步,这样即使重新加载应用程序后,您也可以恢复用户状态。

相关问题