简体   繁体   English

如何允许用户在 React 中编辑网站的一部分?

[英]How to allow user to edit a section of a website in React?

I'm new to web development and I can't seem to find much information on this.我是 web 开发的新手,我似乎找不到太多关于此的信息。

So me and my project group are building an e-commerce website using React.所以我和我的项目组正在使用 React 构建一个电子商务网站。 So far there are 'customer users' and a few 'admin users'.到目前为止,有“客户用户”和一些“管理员用户”。 The website is one single application and the different user types (customer and admin) will be separated by private routes so they will each have pages only that user type can access.该网站是一个单一的应用程序,不同的用户类型(客户和管理员)将由私有路由分隔,因此它们每个都有只有该用户类型可以访问的页面。

Im going to have a page called "Terms and conditions" that will hold a big piece of text containing the companies terms and conditions.我将有一个名为“条款和条件”的页面,其中包含一大段包含公司条款和条件的文本。 I want the admin user to be able to edit the text within that page (component) from another page (component).我希望管理员用户能够从另一个页面(组件)编辑该页面(组件)中的文本。

For me to do this, will I need to store that editable text as a string in a database and edit it there, or is there a way of storing it within the application?为此,我是否需要将该可编辑文本作为字符串存储在数据库中并在那里进行编辑,或者有没有办法将其存储在应用程序中? Storing it in the application is the preferred option.将其存储在应用程序中是首选选项。

Long story short I want an admin user to be able to edit sections of text within the website and the customers users to be able to see the results of the edit.长话短说,我希望管理员用户能够编辑网站内的文本部分,并且客户用户能够看到编辑的结果。

The resource you need to be editable needs to be stored somewhere.您需要可编辑的资源需要存储在某个地方。 Whether that is a database or just a Markdown file you are reading - it's up to you.无论是数据库还是您正在阅读的 Markdown 文件 - 这取决于您。

Then once you have that information stored, you can request it from your frontend application.然后,一旦您存储了该信息,您就可以从您的前端应用程序中请求它。 Users will be able to navigate to a page where that resource will be requested and shown.用户将能够导航到将请求并显示该资源的页面。

In your admin interface you can also create a view in which you load the contents you want to edit and then send the modified back to where it was originally stored.在您的管理界面中,您还可以创建一个视图,在该视图中加载要编辑的内容,然后将修改后的内容发送回最初存储的位置。

That is, you can write to the file or send a POST request to your backend which will write it to your database, so that when users will request it again it will contain the updated, edited information.也就是说,您可以写入文件或向后端发送 POST 请求,后端会将其写入您的数据库,这样当用户再次请求它时,它将包含更新的、已编辑的信息。

The same principle can be applied to other types of resources as well - eg for structured data you can use a simple JSON file that you read and then write or use a database.同样的原则也可以应用于其他类型的资源——例如,对于结构化数据,您可以使用一个简单的 JSON 文件,您可以读取然后写入或使用数据库。

Based on your question you are most likely just starting out especially with the backend side of things, you might want to look into solutions like Firebase where you can consume and manipulate data right from your frontend code in a rather easy manner.根据您的问题,您很可能刚刚开始,尤其是后端方面,您可能想要研究像 Firebase 这样的解决方案,您可以在其中以相当简单的方式直接从前端代码使用和操作数据。

I do this in a deck building website.我在一个甲板建筑网站上这样做。 There are two options you can choose from and it depends on if you want use use Redux or not.您可以选择两个选项,这取决于您是否要使用 Redux。

No matter which direction you choose, the solution is similar.无论您选择哪个方向,解决方案都是相似的。 The only thing that does change is where the logic is.唯一改变的是逻辑在哪里。 With redux it is going to be in a reducer.使用 redux 它将在减速器中。 Without it, the logic will be in your component.没有它,逻辑将在您的组件中。 I personally like using redux because if your UI changes, you have less to de-tangle to get your site working again and it decouples components from children due to the useSelector hook being available to pull redux values in parent components and children components easily without contaminating your props or building a Context provider.我个人喜欢使用 redux,因为如果您的 UI 发生变化,您无需纠结以使您的网站再次运行,并且由于useSelector挂钩可用于拉取父组件和子组件中的 redux 值而不会造成污染,因此它可以将组件与子组件分离您的道具或构建上下文提供程序。

You will need a click away listener component like so https://www.npmjs.com/package/react-click-away-listener .您将需要一个点击离开的侦听器组件,例如https://www.npmjs.com/package/react-click-away-listener

Without redux:不带 redux:

import * as React from 'react'
import ClickAwayListener from 'react-click-away-listener'

const MyComponent = (props) => {
  const [isEditingField, setIsEditingField] = React.useState(false)
  const [value, setValue] = React.useState('')

  const handleClick = (event) => {
    setIsEditingField(true)
  }

  const handleClickAway = (event) => {
    setIsEditingField(false)
  }

  return <div>
    {editing && <ClickAwayListener>
      <input
        type="text"
        onChange={setValue}
        onClick={handleOnClick}
      />
    </ClickAwayListener>
  </div>}

  {!editing && <p>
    {value}
  </p>}
}

Keep in mind that you will probably be debouncing your input with some kind of hook like this one below.请记住,您可能会使用如下所示的某种钩子来消除输入的抖动。 This stops the input from doing a web request every time the user types into the input.这会阻止输入在每次用户输入输入时执行 web 请求。

import * as React from 'react'

export const useDebounce = (
  value,
  setValue,
  timeout = 100,
) => {
  React.useEffect(() => {
    const timeoutId = setTimeout(() => setValue(), timeout)

    return () => clearTimeout(timeoutId)
  }, [value])
}

You will also want to look into short-circuit conditional rendering in React.您还需要研究 React 中的短路条件渲染。 This is when you do the technique above in the return value of that component.这是当您在该组件的返回值中执行上述技术时。 ie {booleanValue && <JSX /> or {!booleanValue && <JSX />} .{booleanValue && <JSX />{!booleanValue && <JSX />} It is a nifty way to have very clean render code in React.在 React 中拥有非常干净的渲染代码是一种很好的方式。

If you do decide to use redux, you would need to do a lot more than this.如果您决定使用 redux,您需要做的远不止这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM