小编soo*_*kie的帖子

即使组件树没有更改,我如何确保 React 创建组件的新实例?

React 根据树中元素的名称更新其组件树。

例如:

<div>
  <MyComponent/>
</div>
Run Code Online (Sandbox Code Playgroud)

和:

<div>
  <MyComponent enabled/>
</div>
Run Code Online (Sandbox Code Playgroud)

...导致 React 使用相同的<MyComponent>实例(因为组件名称没有改变)。这非常有用,因为它确保组件实例内的内部状态持续存在。

请参阅文档了解更多详细信息。

我的问题:“有没有办法强制在某些情况下创建新实例?”

因此,MyComponent如果(比方说)发生了变化,我希望实例化一个新实例,而不是使用相同的实例prop 'x'

javascript reactjs

3
推荐指数
1
解决办法
386
查看次数

禁用所有 material-ui 组件的拼写检查

有没有办法全局禁用material-ui组件的拼写检查?

在使用该material-ui库之前,我使用以下代码段禁用所有新创建的 DOM 元素的拼写检查:

const disableSpellCheck = function(mutations) 
{
    const mutationCount = mutations.length;

    for (let i = 0; i < mutationCount; i++) {
        const mutation = mutations[i];
        if (mutation.attributeName === "spellcheck") {
            const addedNodes = mutation.addedNodes;
            const nodeCount = addedNodes.length;

            for (let n = 0; n < nodeCount; n++) {
                addedNodes[n].setAttribute("spellcheck", "false");
            }
        }
    }
}

const observer = new MutationObserver(disableSpellCheck);

observer.observe(document.getElementById('root'), {
    childList: true, 
    subtree: true,
    attributes: true, 
    attributeFilter: ['spellcheck']
});
Run Code Online (Sandbox Code Playgroud)

这似乎不适用于material-ui. 因为在应用程序范围内禁用拼写检查至关重要,所以我正在寻找一种不涉及单独修改每个组件的样式的解决方案。

javascript reactjs material-ui

3
推荐指数
1
解决办法
1680
查看次数

从基类构造函数访问子构造函数

给定以下类层次结构:ChildClassextends ParentClass,是否可以ChildClass从构造函数访问构造函数ParentClass?例如:

class ChildClass extends ParentClass
{
    constructor()
    {
        super()
    }
}

ChildClass.prop = 'prop'

class ParentClass
{
    constructor()
    {
        if (this._child().prop == 'prop') // here
        {
            console.log("All ok");
        }
    }

    get _child()
    {
        return this.constructor;
    }
}
Run Code Online (Sandbox Code Playgroud)

换句话说,我正在尝试做的是访问孩子的'静态'属性以进行验证

javascript ecmascript-6

2
推荐指数
1
解决办法
79
查看次数

在ES6中导出/导入内置对象的自定义功能?

我有一个'自定义'目录,我想存储对内置对象原型的任何更改.每个被修改的内置对象都有自己的文件(即custom/String.js任何修改String.prototype).

除了这些文件之外,我还将调用一个文件custom/All.js来导出要使用的自定义功能.

All.js

export * from './String'
export {Multiply} from './Array'
Run Code Online (Sandbox Code Playgroud)

main.js

import * from './custom/All'
Run Code Online (Sandbox Code Playgroud)

String.js

// something like this
export String.prototype.doSomething = function() {}
Run Code Online (Sandbox Code Playgroud)

可以这样做吗?

javascript ecmascript-6 es6-modules

2
推荐指数
1
解决办法
1547
查看次数

Python 3 - 将多个API请求中的JSON解析为列表并输出到文件

我如何能...

1)从Python 3中的API查询中解析JSON对象

2)将多个请求解析为列表,并且

3)将列表输出到JSON文件中

python api json python-3.x

1
推荐指数
1
解决办法
1940
查看次数

static_cast从基类指向派生类指针是无效的

我正在创建一个简单的测试实体组件系统.我有一个基Component类与几个派生类.然后我有几个系统将一些逻辑应用于这些组件.

// Component.h
// ------------
class Component
{
public:
    Component();
    ~Component();
}


// ControlComponent.h
// -------------------
#include <string>
#include "Component.h"

class ControlComponent : public Component
{
public:
    std::string input = ""; // store simple input instruction
    ControlComponent();
    ~ControlComponent();
};


// ControlSystem.cpp
void ControlSystem::update(Entity* entity)
{
    vector<Component*>* components = entity->getComponents();

    for (Component* component : *components)
    {
        PositionComponent* pc = static_cast<PositionComponent*>(component);
        ControlComponent* cc = static_cast<ControlComponent*>(component);

        if (pc != nullptr && cc != nullptr)
        {
            std::cout << "Which direction would you …
Run Code Online (Sandbox Code Playgroud)

c++ pointers static-cast

1
推荐指数
2
解决办法
1362
查看次数