当前位置:网站首页>Umijs - data transmission between main and sub applications of Qiankun
Umijs - data transmission between main and sub applications of Qiankun
2022-07-23 15:58:00 【Zong_ 0915】
UmiJs - qiankun Between master and child applications , Data transfer
One . Sending data in the main application
import {
initGlobalState } from 'qiankun';
// Mainly through setGlobalState To put objects into a global common to master and child applications State in .
const {
setGlobalState } = initGlobalState({
});
// Case study
setGlobalState({
userName: window.atob(userName),
age: 10,
});
Two . Receiving data in sub application
import React, {
useState, useEffect } from 'react';
// Need to use useModel To get the data transferred in the main application
import {
useModel } from 'umi';
// Get the parameters passed between master and child applications
const getMainSubProps = () => {
// The name is fixed @@qiankunStateFromMaster
const masterProps = useModel('@@qiankunStateFromMaster');
// You can use one State De storage , In this way, other places only need to introduce this getMainSubProps Components , Get the returned result .
const [ name, setUserName ] = useState<string>([]);
useEffect(() => {
// We need to add ? Of , Prevent null pointer exception
masterProps?.onGlobalStateChange((state: any) => {
const {
userName ,age } = state;
if(userName){
setUserName(userName);
}
});
}, []);
return {
name };
};
export default getMainSubProps;
In sub applications , Just write a basic component , Avoid repeated monitoring . Just listen to the updated value . If you need to use this component on other pages , The pseudocode is as follows :
import getMainSubProps from 'xxx';
const {
age,name} = getMainSubProps();
边栏推荐
猜你喜欢
随机推荐
记一次SQL优化
什么是真正的 HTAP ?(二)挑战篇
后缀表达式(暑假每日一题 4)
如何成为一个优雅的硬件工程师?
xxl-job 实现email发送警告的代码解析(一行一行代码解读)
[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~
C语言书籍推荐
List merging (summer vacation daily question 3)
Opnsense - multifunctional, highly reliable and easy-to-use firewall (II)
C语言书写规范
太拼了!腾讯T4大佬凌晨4点还在熬夜,竟是在整理这分布式事务笔记
C # calculate the number of times a character appears in the string
redis 哨兵模式
浅谈‘过早优化’
aws篇3 go语言如何publish message 到iot的MQTT
Software testing weekly (No. 81): what can resist negativity is not positivity, but concentration; What can resist anxiety is not comfort, but concrete.
Camera flashlight modification
【无标题】
As a tester, you cannot fail to understand ADB commands and operations
[paper study] source mixing and separation robot audio steganography









