Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

javascript - Retrieve a string from an EditorBlock within a custom ComponentBlock with React and Draft.js

I am trying to build a custom Draft.js ComponentBlock to accept an image URL in order to display an image within the Editor component. I tried using an tag and a "useState" hook, updated via a function onChange, that could store the input tag's value until the user was done typing/pasting the URL. However, the input tag was unusable because the text written in the input field would not appear in the input field, but would be stored in the EditorState until about 5 characters into typing, and pasting wouldn't display at all. I did some looking and found this:

https://draftjs.org/docs/advanced-topics-block-components/

which suggests using an "EditorBlock" component instead. However, while on the screen the component displays text just fine, I'm struggling to find a way to retrieve the data that is entered into the EditorBlock component. See in the code below.

Thank you!

import React, {useState} from 'react';
import { EditorBlock } from 'draft-js';

export function ImageButton(props){

    const [ imageURL, setImageURL ] = useState("");
    const [ insertURL, setInsertURL ] = useState("");
    
    console.log(insertURL);

    function handleSelect(){
        setImageURL(insertURL)
    }

    if(imageURL == ""){
        return(
            <div>
                <p>Insert URL:</p>
                {/* <input
                    value={insertURL}
                    onChange={(evt) => {handleInputChange(evt, setInsertURL)}}
                /> */}
                <div>
                    <EditorBlock {...props} onChange={(env) => setInsertURL(env.target.value)}/>
                </div>
                
                <button onClick={handleSelect}>Select</button>
            </div>
        )
    }
    else{
        return(
            <div>
                <img src={imageURL} />
            </div>
        )
    }
}
question from:https://stackoverflow.com/questions/65911472/retrieve-a-string-from-an-editorblock-within-a-custom-componentblock-with-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved it, here's the new code

import React, {useState} from 'react';

import { EditorBlock } from 'draft-js';

export function ImageButton(props){

const [ imageURL, setImageURL ] = useState("");
//const [ insertURL, setInsertURL ] = useState("");
const propsData = Object.entries(props);

console.log(propsData[1][1]._map._root.entries[1][1])

function handleSelect(data){
    setImageURL(data)
}

if(imageURL == ""){
    return(
        <div>
            <p>Insert URL:</p>

            <div>
                <EditorBlock {...props}/>
            </div>
            
            <button onClick={() => handleSelect(propsData[1][1]._map._root.entries[1][1])}>Select</button>
        </div>
    )
}
else{
    return(
        <div>
            <img src={imageURL} />
        </div>
    )
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...