javascript - 如何在 react 中制作阅读更多按钮?

标签 javascript css reactjs dom jsx

我正在为太长的文本区域制作一个阅读更多按钮。它给我一个错误 期待一个赋值或函数调用,却看到了一个表达式

这是代码

readMore = () => {
   var showResult = false; 
   if (showResult === false) {
      return  <div className = "futura-16-300" style = {{textAlign : "left"}}>
      Welcome to level 1 of the whiplash phyx. This phyx is made for those who have suffered from a minor whiplash from a car accident and have been cleared by their medical professional to start physical rehab..
      <a onClick={() => {showResult = true}} className="futura-16-300">Read more</a> 
      </div>
    } else if (showResult === true) {
      <div className="futura-16-300" style = {{textAlign : "left"}}>
      If you are unsure about your diagnosis, feel free to consult your doctor or book a phyxable consultation with one of our practitioners online. After suffering from a minor fender bender, your neck muscles become reactively tight. Your posture muscles are still in shock from the collision, and the stress of the awful event will make those muscles even tighter. Fear not, if done properly, this phyx will provide you with relief, while teaching you how to combat pain and stiffness from coming back. Like most of the phyxable solutions, there are 3 levels to this phyx. We start off with a very simple and relaxing Level 1 to get the muscles to calm down. Level two improves your posture and further diminishes the pain and tightness. If you are successful, level 3 focuses on getting your neck and mid-back muscles back to and even stronger than what they were prior to the accident. This phyx should be performed once a day. Each session of each level will progressively become more challenging. Feel free to stay at a particular session if you felt it was too challenging, or if there is more than a moderate amount of soreness a day after the session. But if you are up for the challenge, most of our users finish level 1 within one week.
      <a onClick={() => {showResult = false}} className="futura-16-300 ">Read less</a> 
      <br/>
      </div>
      } 
  }
{this.readMore()}

最佳答案

这是您问题的有效解决方案 -

https://codesandbox.io/s/react-example-kouyx

您的代码片段存在多个问题。

  • 您的问题:expected an assignment or function call and instead saw an expression , 是因为else条件下,没有return语句。有一个悬垂的 <div>它从未分配给变量,也未返回。
  • 此外,应该在状态中使用 showResult 来管理 Read MoreRead Less特色。

希望解决方案可以通过 codesandbox 链接进行解释。我添加了一个状态处理程序,还添加了 return 语句来修复它。

关于javascript - 如何在 react 中制作阅读更多按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58250471/

相关文章:

javascript - 单击 li 标签时无法将 css 添加到 li 元素

jquery - 如何使 hc-sticky 插件引用其容器

ruby-on-rails - Webpacker::Manifest::MissingEntryError

reactjs - 当值来自 redux 并在子进程中更新时,React Navigation : How to update navigation title for parent,?

javascript - Jquery点击函数无法识别

javascript - 在 Promise 中调用函数

javascript - 将 JSInterop 的 ExampleJsInterop JavaScript 代码转换为 TypeScript

jquery - Css子菜单全宽,两列不起作用

javascript - 如何将 knex 与异步/等待一起使用?

javascript - 在react中使用map函数并将数据解析到子组件