admin管理员组文章数量:1022695
I have been testing with SunEditor since I want to pre-load an HTML text stored in DB and let users modify it if necessary. I'm passing a ref variable from parent so when the submit button is clicked he could get the value and save modificacions. setContents is just working sometimes. When I save the project and is piled again text appeared. But if I use the app or refresh the window text dissapear. I have checked that the variable still has the value. I'm new to React and I'm not sure If I'm doing it wrong or just suneditor-react failing. Can you help me?
Here is my code:
export const TranslationArea = React.forwardRef((props, ref) =>{
const handleEditorChange = (value) => {
console.log(value);
ref.current= value;
}
const handleClick = () => {
console.log(ref.current);
}
return(
<div>
<h2>{props.title}</h2>
<SunEditor
autoFocus={true}
width="100%"
height="150px"
setOptions={{
buttonList: [
// default
['undo', 'redo'],
['bold', 'underline', 'italic', 'list'],
['table', 'link', 'image'],
['fullScreen']
]
}}
setContents={props.content}
onChange={handleEditorChange}
/>
<div>{props.content}</div>
<button onClick={handleClick}>Content</button>
</div>
);
});
Here is the screenshot with content properly loaded inside SunEditor div (only when piling project):
If I refresh the page or navigate to the same link...
I have displayed a div with the same props.content just to check that fowardRef is working. Why setContents is now working? I have inspect with React tools and the property is loaded:
Any idea?
I have been testing with SunEditor since I want to pre-load an HTML text stored in DB and let users modify it if necessary. I'm passing a ref variable from parent so when the submit button is clicked he could get the value and save modificacions. setContents is just working sometimes. When I save the project and is piled again text appeared. But if I use the app or refresh the window text dissapear. I have checked that the variable still has the value. I'm new to React and I'm not sure If I'm doing it wrong or just suneditor-react failing. Can you help me?
Here is my code:
export const TranslationArea = React.forwardRef((props, ref) =>{
const handleEditorChange = (value) => {
console.log(value);
ref.current= value;
}
const handleClick = () => {
console.log(ref.current);
}
return(
<div>
<h2>{props.title}</h2>
<SunEditor
autoFocus={true}
width="100%"
height="150px"
setOptions={{
buttonList: [
// default
['undo', 'redo'],
['bold', 'underline', 'italic', 'list'],
['table', 'link', 'image'],
['fullScreen']
]
}}
setContents={props.content}
onChange={handleEditorChange}
/>
<div>{props.content}</div>
<button onClick={handleClick}>Content</button>
</div>
);
});
Here is the screenshot with content properly loaded inside SunEditor div (only when piling project):
If I refresh the page or navigate to the same link...
I have displayed a div with the same props.content just to check that fowardRef is working. Why setContents is now working? I have inspect with React tools and the property is loaded:
Any idea?
Share Improve this question asked Oct 14, 2020 at 8:20 Adrián RodriguezAdrián Rodriguez 44012 silver badges28 bronze badges2 Answers
Reset to default 3I'm passing a ref variable from parent so when the submit button is clicked he could get the value and save modifications.
If all you need to access is the value, let's pass down a callback instead. We'll call it onSumbit
and it will be a function which takes the string
value from the SunEditor
ponent.
In order to have access to the current edited value, I am using a local state in the TranslationArea
ponent and updating that state through the editor's onChange
method. Then when the submit button is clicked, I call onSubmit
with the value of content
from the local state. (There might be another way to do this but I'm not familiar with SunEditor).
The TranslationArea
ponent looks like this:
export const TranslationArea = ({initialContent = "", title, onSubmit}) => {
const [content, setContent] = useState(initialContent);
return(
<div>
<h2>{title}</h2>
<SunEditor
autoFocus={true}
width="100%"
height="150px"
setOptions={{
buttonList: [
// default
['undo', 'redo'],
['bold', 'underline', 'italic', 'list'],
['table', 'link', 'image'],
['fullScreen']
]
}}
setContents={initialContent}
onChange={setContent}
/>
<div>{content}</div>
<button onClick={() => onSubmit(content)}>Submit</button>
</div>
);
};
I tested it in my CodeSandbox by giving it an onSubmit
function which logs the submitted content, but you can do whatever you need to with it.
export default function App() {
const initialContent = "Hello World";
const onSubmit = (content: string) => {
console.log("Submitted Content", content);
};
return (
<div className="App">
<TranslationArea
initialContent={initialContent}
onSubmit={onSubmit}
title="Edit Text"
/>
</div>
);
}
CodeSandbox Link
const [content,setContent] = useState('');
var initialContent = YOUR_PROPS_FROM_DB_STATE;
In SunEditor Component:
<SunEditor
defaultValue={initialContent}
setContents={YOUR_PROPS_FROM_DB_STATE}
placeholder={placeholder}
onChange={setContent}
/>
In my case YOUR_PROPS_FROM_DB is state es from parent ponent. placeholder - any variable or string inside. Sure that is not the best solution. But somehow it works.
Hope this helps! Regards,
I have been testing with SunEditor since I want to pre-load an HTML text stored in DB and let users modify it if necessary. I'm passing a ref variable from parent so when the submit button is clicked he could get the value and save modificacions. setContents is just working sometimes. When I save the project and is piled again text appeared. But if I use the app or refresh the window text dissapear. I have checked that the variable still has the value. I'm new to React and I'm not sure If I'm doing it wrong or just suneditor-react failing. Can you help me?
Here is my code:
export const TranslationArea = React.forwardRef((props, ref) =>{
const handleEditorChange = (value) => {
console.log(value);
ref.current= value;
}
const handleClick = () => {
console.log(ref.current);
}
return(
<div>
<h2>{props.title}</h2>
<SunEditor
autoFocus={true}
width="100%"
height="150px"
setOptions={{
buttonList: [
// default
['undo', 'redo'],
['bold', 'underline', 'italic', 'list'],
['table', 'link', 'image'],
['fullScreen']
]
}}
setContents={props.content}
onChange={handleEditorChange}
/>
<div>{props.content}</div>
<button onClick={handleClick}>Content</button>
</div>
);
});
Here is the screenshot with content properly loaded inside SunEditor div (only when piling project):
If I refresh the page or navigate to the same link...
I have displayed a div with the same props.content just to check that fowardRef is working. Why setContents is now working? I have inspect with React tools and the property is loaded:
Any idea?
I have been testing with SunEditor since I want to pre-load an HTML text stored in DB and let users modify it if necessary. I'm passing a ref variable from parent so when the submit button is clicked he could get the value and save modificacions. setContents is just working sometimes. When I save the project and is piled again text appeared. But if I use the app or refresh the window text dissapear. I have checked that the variable still has the value. I'm new to React and I'm not sure If I'm doing it wrong or just suneditor-react failing. Can you help me?
Here is my code:
export const TranslationArea = React.forwardRef((props, ref) =>{
const handleEditorChange = (value) => {
console.log(value);
ref.current= value;
}
const handleClick = () => {
console.log(ref.current);
}
return(
<div>
<h2>{props.title}</h2>
<SunEditor
autoFocus={true}
width="100%"
height="150px"
setOptions={{
buttonList: [
// default
['undo', 'redo'],
['bold', 'underline', 'italic', 'list'],
['table', 'link', 'image'],
['fullScreen']
]
}}
setContents={props.content}
onChange={handleEditorChange}
/>
<div>{props.content}</div>
<button onClick={handleClick}>Content</button>
</div>
);
});
Here is the screenshot with content properly loaded inside SunEditor div (only when piling project):
If I refresh the page or navigate to the same link...
I have displayed a div with the same props.content just to check that fowardRef is working. Why setContents is now working? I have inspect with React tools and the property is loaded:
Any idea?
Share Improve this question asked Oct 14, 2020 at 8:20 Adrián RodriguezAdrián Rodriguez 44012 silver badges28 bronze badges2 Answers
Reset to default 3I'm passing a ref variable from parent so when the submit button is clicked he could get the value and save modifications.
If all you need to access is the value, let's pass down a callback instead. We'll call it onSumbit
and it will be a function which takes the string
value from the SunEditor
ponent.
In order to have access to the current edited value, I am using a local state in the TranslationArea
ponent and updating that state through the editor's onChange
method. Then when the submit button is clicked, I call onSubmit
with the value of content
from the local state. (There might be another way to do this but I'm not familiar with SunEditor).
The TranslationArea
ponent looks like this:
export const TranslationArea = ({initialContent = "", title, onSubmit}) => {
const [content, setContent] = useState(initialContent);
return(
<div>
<h2>{title}</h2>
<SunEditor
autoFocus={true}
width="100%"
height="150px"
setOptions={{
buttonList: [
// default
['undo', 'redo'],
['bold', 'underline', 'italic', 'list'],
['table', 'link', 'image'],
['fullScreen']
]
}}
setContents={initialContent}
onChange={setContent}
/>
<div>{content}</div>
<button onClick={() => onSubmit(content)}>Submit</button>
</div>
);
};
I tested it in my CodeSandbox by giving it an onSubmit
function which logs the submitted content, but you can do whatever you need to with it.
export default function App() {
const initialContent = "Hello World";
const onSubmit = (content: string) => {
console.log("Submitted Content", content);
};
return (
<div className="App">
<TranslationArea
initialContent={initialContent}
onSubmit={onSubmit}
title="Edit Text"
/>
</div>
);
}
CodeSandbox Link
const [content,setContent] = useState('');
var initialContent = YOUR_PROPS_FROM_DB_STATE;
In SunEditor Component:
<SunEditor
defaultValue={initialContent}
setContents={YOUR_PROPS_FROM_DB_STATE}
placeholder={placeholder}
onChange={setContent}
/>
In my case YOUR_PROPS_FROM_DB is state es from parent ponent. placeholder - any variable or string inside. Sure that is not the best solution. But somehow it works.
Hope this helps! Regards,
本文标签: javascriptSetContents SunEditor is not working ReactStack Overflow
版权声明:本文标题:javascript - SetContents SunEditor is not working React - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745563338a2156296.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论