小编mdz*_*kon的帖子

有没有办法判断ReactElement是否呈现"null"?

在我的JSX中,我有一个条件渲染逻辑的情况 - 当元素A渲染某些东西(它的render()函数返回除了之外null),然后渲染元素B,就在元素A的上方.

代码示例(简化)如下所示:

function render() {
    let elemA = (<ElementA someProp={this.someVar} />);

    if (elemA.isNull()) {
        return (
            <div>
                { ...someElements }
            </div>
        );
    }

    return (
        <div>
            { ...someElements }
            <ElementB />
            { elemA }
        </div>
    );
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是 - 有没有办法elemA.isNull()检查?

javascript reactjs react-jsx

12
推荐指数
3
解决办法
7367
查看次数

TS1055使用async/await时使用类型别名

我已经声明了一个类型别名:

export type ActivationPromise = Promise<void>;
Run Code Online (Sandbox Code Playgroud)

我写了以下函数:

async function derp(): ActivationPromise {
    await test();
}

function test() : ActivationPromise {
    return Promise.resolve();
}
Run Code Online (Sandbox Code Playgroud)

我的tsconfig.json:

{
    "compilerOptions": {
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "module": "commonjs",
        "target": "es5",
        "allowJs": true,
        "alwaysStrict": true,
        "importHelpers": true,
        "lib": [ "dom", "es5", "es2015.promise", "es2015.iterable", "scripthost"]
    },
    "compileOnSave": false
}
Run Code Online (Sandbox Code Playgroud)

当我编译时,我得到:

错误TS1055:类型'ActivationPromise'不是ES5/ES3中的有效异步函数返回类型,因为它不引用与Promise兼容的构造函数

如果我将返回类型更改为简单Promise<void>,则代码编译正常.仅在使用类型别名时,才会出现问题.由于类型别名应该只是作为一个typedef,为什么会发生这种情况?

typescript

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

防止<amp-sidebar>返回页面顶部

我正在尝试为我以前的汉堡菜单创建一个简单的替代品,它有一些导航链接,只需使用将页面视图滚动到指定的部分href="#section".

由于我不能再使用Checkbox技巧,我不得不使用<amp-sidebar>它:

<amp-sidebar id="sidebar" layout="nodisplay" side="right">
    <ul>
        <li>
            <a href="#secion1">Section 1</a>
        </li>
        <li>
            <a href="#secion2">Section 2</a>
        </li>
        <li>
            <a href="#secion3">Section 3</a>
        </li>
    </ul>
</amp-sidebar>
Run Code Online (Sandbox Code Playgroud)

问题是,每一次的侧边栏被关闭,页面获取滚动回到它的顶部位置(甚至URL被恢复到原来的状态,所以#section被删除).

有什么方法可以防止这种行为吗?

amp-html

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

NVM:如何全局设置节点版本?

使用Node Version Manager时,我可以将节点的版本设置为当前目录中的最新版本nvm use node.如何指定使用哪个版本?

javascript npm nvm

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

在React中将setInterval添加到componentDidMount

我想每1000毫秒更新一次React组件的状态。但是,我尝试在setInterval上进行操作componentDidMount,但是没有运气。目前,我有两个结果console.log,一个是构造函数中的空状态对象,另一个是从API提取的对象。如何使用setInterval每隔1000 ms更新一次组件的状态?

这是我的代码:

let url = 'some-link-bla-bla';

class Basemap extends React.Component {

    constructor(props) {
        super(props);
        this.state = {};
        console.log(this.state);
    }

    render() {
        return (
            <Scene style={{ width: '100vw', height: '100vh' }} 
                    mapProperties={{ basemap: 'satellite' }} 
                    viewProperties={ this.state } />
        );
    }

    componentDidMount() {
        fetch(url)
            .then(d => d.json().then(function(d) {
                console.log(d);
            }))
            .then(d => function(d) {
                this.setState({
                  center: [
                      {latitude : d.iss_position.latitude} + ', ' + 
                      {longitude: d.iss_position.longitude}
                    ]
                })
            });
    }
}

export default Basemap;
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

标签 统计

javascript ×3

reactjs ×2

amp-html ×1

npm ×1

nvm ×1

react-jsx ×1

typescript ×1