function setAppState(rootNode, globalAttributeName, newValue) {
var $root = $(rootNode)._reactRootContainer;
var reactState = false;
var searchStack = [];
var searched = [];
// Fetch the "_internalRoot" attribute from the DOM node.
for ( const key in $root ) {
if (0 === key.indexOf('_internalRoot') ) {
searchStack.push($root[key]);
break;
}
}
// Find the state by examining the object structure.
while (searchStack.length) {
var obj = searchStack.pop();
if (obj && typeof obj == 'object' && -1 === searched.indexOf(obj)) {
if ( undefined !== obj[ globalAttributeName ] ) {
reactState = obj;
break;
}
for (i in obj) {
searchStack.push(obj[i]);
}
searched.push(obj);
}
}
// Take the last pushed object as it contains the State to be changed
state = searched[searched.length - 1].memoizedState;
state[globalAttributeName] = newValue;
return state;
}
``` `rootNode` 将是react应用程序根元素的id,例如。
# app `globalAttributeName` 将是你需要改变的关键 `newValue` 将是该钥匙的新状态
1条答案
按热度按时间lf5gs5x21#
好的,在访问第三方react应用程序状态的帮助下,我终于可以自己做了
修改了此处答案中的代码以更改状态: