我有一个 useEffect 钩子,它将执行 axios get api 请求并存储在 useState
const [apiResponse, setApiResponse] = useState([]);
useEffect(() => {
... // inside of .then()
setApiResponse(data.data.payload);
....
},[]);
apiResponse 有一个 Object 数组
[
{
userName: "user Name",
description: "<p>lorem ipsum lorem ipsum lorem ipsum</p>",
selected: "Yes",
}
]
反应草案所见即所得
const blocksFromHTML = convertFromHTML(`${apiResponse.description}`);
const state = ContentState.createFromBlockArray(
blocksFromHTML.contentBlocks,
blocksFromHTML.entityMap
);
const [editorState, setEditorState] = useState(() =>
EditorState.createWithContent(state)
);
const onEditorStateChange = (editorState) => {
setEditorState(editorState);
return draftToHtml(convertToRaw(editorState.getCurrentContent()));
};
....
return (
....
<Editor
toolbarClassName="toolbarClassName"
wrapperClassName="ChannelDesscription"
editorClassName="editorClassName"
editorState={editorState}
defaultEditorState={editorState}
onEditorStateChange={onEditorStateChange}
/>
...
)
我在编辑器上打印未定义。
在控制台日志上,我在第一次渲染时未定义,然后在控制台上打印该值..但在编辑器中它仍然未定义。
console.log(apiResponse.description)
我在这里犯了什么错误?
谢谢你。