小编Nic*_*ick的帖子

反应-this.input.value vs句柄更改

什么是更好的?

我有一个带有10个输入的表单。

我应该使用this.input.value或处理更改并将其存储在其中state吗?

handleChange(e) {
    this.setState({input: e.target.value});
}
...
<input type="text" value={this.state.input} onChange={this.handleChange} />
Run Code Online (Sandbox Code Playgroud)

要么

onSubmit() {
    const inputValue = this.input.value;
    ...
}
...
<input type="text" ref={(input) => {this.input = input;}} />
Run Code Online (Sandbox Code Playgroud)

从文档中:

何时使用参考

There are a few good use cases for refs:

    Managing focus, text selection, or media playback.
    Triggering imperative animations.
    Integrating with third-party DOM libraries.

Avoid using refs for anything that can be done declaratively.
Run Code Online (Sandbox Code Playgroud)

reactjs

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

Draft.js-添加未选择文本的链接

如何添加链接?我知道如何将链接添加到选择

          const contentState = editorState.getCurrentContent();
          const contentStateWithEntity = contentState.createEntity(
            'LINK',
            'MUTABLE',
            {url: urlValue}
          );
          const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
          const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
          this.setState({
            editorState: RichUtils.toggleLink(
              newEditorState,
              newEditorState.getSelection(),
              entityKey
            )}
Run Code Online (Sandbox Code Playgroud)

我们得到选择newEditorState.getSelection()并添加链接。

但是如何添加链接而不选择呢?a如果未选择文本,仅添加带有文本的新标签?如果我不选择任何文本,则不会添加任何内容。

draftjs

4
推荐指数
2
解决办法
2289
查看次数

标签 统计

draftjs ×1

reactjs ×1