【问题标题】:How do i render data from local storage in react typescript?如何在反应打字稿中呈现本地存储中的数据?
【发布时间】:2020-12-03 16:29:46
【问题描述】:

当我点击保存时,我希望数据显示在我的保存按钮下方的浏览器中,我该怎么做?我正在使用对打字稿模板做出反应。

 function ButtonDefaultExample(props: IButtonExampleProps) {
  const { disabled, checked } = props;
  const [showEditor, setShowEditor] = useState(false);
  const [showLesson, setShowLesson] = useState(false);

  return (
    <div style={{width: '80%', margin:'0px auto'}}>
      <h1>Click to create a new course.</h1>
      <Stack horizontal tokens={{childrenGap:10}} horizontalAlign="center" style={{width:'100%'}}>
        <DefaultButton text="Create Course" onClick={()=>setShowLesson(true)} allowDisabledFocus disabled={disabled} checked={checked} />
      </Stack>
      <br><br>
      {
        showLesson && (
          <DefaultButton text="Create Lesson" onClick={()=>setShowEditor(true)} allowDisabledFocus disabled={disabled} checked={checked} style={{alignItems:'center'}} />
        )
      }
      {
        showEditor && (
          <SunEditor onChange={ (content: string) => {
            localStorage.setItem("Contents", content);
          } } />
        )
      }
      <DefaultButton text="Save" onClick={() => localStorage.getItem("Contents") } allowDisabledFocus disabled={disabled} checked={checked} />
    </div>
  );
}

【问题讨论】:

    标签: javascript reactjs typescript react-tsx react-typescript


    【解决方案1】:

    也许你可以把它保存在一个状态中,然后显示它。

    试试这个

    function ButtonDefaultExample(props: IButtonExampleProps){
      const { disabled, checked } = props;
      const [showEditor, setShowEditor] = useState(false);
      const [showLesson, setShowLesson] = useState(false);
      const [showContent, setShowContent] = useState('');
    
       
    
      return (
        <div style={{width: '80%', margin:'0px auto'}}>
          <h1>Click to create a new course.</h1>
    
          <Stack horizontal tokens={{childrenGap:10}} horizontalAlign="center" style={{width:'100%'}}>
            <DefaultButton text="Create Course" onClick={()=>setShowLesson(true)} allowDisabledFocus disabled={disabled} checked={checked} />
          </Stack>
          <br></br>
          {
            showLesson && (
              <DefaultButton text="Create Lesson" onClick={()=>setShowEditor(true)} allowDisabledFocus disabled={disabled} checked={checked} style={{alignItems:'center'}} />
            )
          }
          {
            showEditor && (
              <SunEditor onChange={ (content: string) => {
                localStorage.setItem("Contents", content);
              } } />
            )
    
          }
         <DefaultButton text="Save" onClick={() => setShowContent(localStorage.getItem("Contents")) } allowDisabledFocus disabled={disabled} checked={checked} />
         {showContent}
        </div>
      );
     }
    

    【讨论】:

    • 在 ""localStorage.getItem("Contents")"" 显示错误 'string | null' 不可分配给“SetStateAction”类型的参数。类型 'null' 不可分配给类型 'SetStateAction'.ts(2345)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2012-11-06
    • 2021-11-08
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多