什么是更好的?
我有一个带有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)
从文档中:
何时使用参考
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.
如何添加链接?我知道如何将链接添加到选择
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如果未选择文本,仅添加带有文本的新标签?如果我不选择任何文本,则不会添加任何内容。