Javascript 编辑文本输入

Javascript 编辑文本输入,javascript,html,css,reactjs,firebase,Javascript,Html,Css,Reactjs,Firebase,是否有任何方法将数据库的文本注入到输入中以进行编辑?不像占位符 这里有一张图片作为例子 在这种情况下,文本被放置为占位符,如果不全部擦除,显然无法编辑 代码如下: <div style={modalStyle} className={classes.paper}> <form className="update-note create-note"> <input

是否有任何方法将数据库的文本注入到输入中以进行编辑?不像占位符

这里有一张图片作为例子

在这种情况下,文本被放置为占位符,如果不全部擦除,显然无法编辑

代码如下:

<div style={modalStyle} className={classes.paper}>
            <form className="update-note create-note">
              <input
                className="input-edit-note"
                name="title"
                onChange={(e) => setTitle(e.currentTarget.value)}
                value={title}
                placeholder={props.title}
              />

              <textarea
                name="content"
                onChange={(e) => setContent(e.currentTarget.value)}
                value={content}
                placeholder={props.content}
              />
              <Fab onClick={submitNote}>
                <AddCircleIcon />
              </Fab>
            </form>
          </div>

setTitle(e.currentTarget.value)}
值={title}
占位符={props.title}
/>
setContent(e.currentTarget.value)}
值={content}
占位符={props.content}
/>

无论您在
value={…}
中输入什么,都可以在文本字段中编辑。 如果您希望与组件本地数据合并,那么我建议通过useState和useEffect进行合并

或者:

const Example = (props) => {
  const [title, setTitle] = useState(props.title);
  ...
}
还是像这样

const Example = (props) => {
  const [title, setTitle] = useState('');
  
  useEffect(() => {
    setTitle(props.title);
  }, [props.title]);
  ...
}
然后使用输入标记中的值

<input
  ...
  value={title}
  ...
/>

示例组件:

// When initiating this component, make sure to pass "title" as props
const Example = (props) => {
  // Title is bound to state within this component
  const [title, setTitle] = useState(props.title);
  
  const _onChangeTitle = e => {
    setTitle(e.target.value);
  }
  
  return (
    <input
      className="input-edit-note"
      name="title"
      onChange={_onChangeTitle}
      value={title}             // title will always be state title
      placeholder={props.title} // props.title will always remain the same
    />
  )
}
//启动此组件时,请确保将“title”作为道具传递
常量示例=(道具)=>{
//标题绑定到此组件中的状态
const[title,setTitle]=useState(props.title);
const\u onChangeTitle=e=>{
设置标题(如目标值);
}
返回(
)
}

无论您在
value={…}
中输入什么,都可以在文本字段中编辑。 如果您希望与组件本地数据合并,那么我建议通过useState和useEffect进行合并

或者:

const Example = (props) => {
  const [title, setTitle] = useState(props.title);
  ...
}
还是像这样

const Example = (props) => {
  const [title, setTitle] = useState('');
  
  useEffect(() => {
    setTitle(props.title);
  }, [props.title]);
  ...
}
然后使用输入标记中的值

<input
  ...
  value={title}
  ...
/>

示例组件:

// When initiating this component, make sure to pass "title" as props
const Example = (props) => {
  // Title is bound to state within this component
  const [title, setTitle] = useState(props.title);
  
  const _onChangeTitle = e => {
    setTitle(e.target.value);
  }
  
  return (
    <input
      className="input-edit-note"
      name="title"
      onChange={_onChangeTitle}
      value={title}             // title will always be state title
      placeholder={props.title} // props.title will always remain the same
    />
  )
}
//启动此组件时,请确保将“title”作为道具传递
常量示例=(道具)=>{
//标题绑定到此组件中的状态
const[title,setTitle]=useState(props.title);
const\u onChangeTitle=e=>{
设置标题(如目标值);
}
返回(
)
}

从数据库中获取数据后,将其保存在组件状态,正如我看到的那样:

const MyComponent = (props) => {
  const [title, setTitle] = useState(props.title);
  ...
}
然后将输入值设置为从数据库接收的标题数据。 在一次更改时,触发
setTitle
函数,该函数将更新您的
title
状态

<input
  className="YourProfileInput"
  placeholder="Insert Title"
  value={title}
  onChange={(e) => setTitle(e.currentTarget.value)}
/>
setTitle(e.currentTarget.value)}
/>

从数据库中获取数据后,将其保存在组件状态,正如我看到的那样:

const MyComponent = (props) => {
  const [title, setTitle] = useState(props.title);
  ...
}
然后将输入值设置为从数据库接收的标题数据。 在一次更改时,触发
setTitle
函数,该函数将更新您的
title
状态

<input
  className="YourProfileInput"
  placeholder="Insert Title"
  value={title}
  onChange={(e) => setTitle(e.currentTarget.value)}
/>
setTitle(e.currentTarget.value)}
/>

有效,但当我第二次尝试编辑便笺时,输入字段显示为空。你说“第二次”是什么意思?有效,但当我第二次尝试编辑便笺时,输入字段显示为空。你说“第二次”是什么意思?抱歉,我删除了上一条消息,现在我删除了占位符,结果是输入字段是空的,不用担心。我将更新一个完整的组件,例如一个小的例子。对不起,我删除了上一个消息,现在我删除了占位符,结果是输入字段是空的,没有问题。我将以一个完整的组件作为例子进行更新。