首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在多个子组件中使用react

在多个子组件中使用react
EN

Stack Overflow用户
提问于 2017-08-29 11:46:25
回答 1查看 3.5K关注 0票数 3

我想在我的应用程序中使用react-joyride,但是有一个问题。我已经将我的组件分解为NavbarSideNavigation。它们是单独的子组件。现在我想先在react-joyride中使用SideNavigation,然后在Navbar中使用最后一步。我怎样才能做到呢?

这是我的密码

代码语言:javascript
运行
复制
class AgentDashboard extends React.Component {
  constructor() {
    super();
    this.state = {
      username: "",
      roles: [],
      joyrideOverlay: true,
      joyrideType: "continuous",
      isReady: false,
      isRunning: false,
      stepIndex: 0,
      steps: [],
      selector: ""
    };
  }
  componentDidMount() {
  this.setState({
      isReady: true,
      isRunning: true,
    });
  }


  addSteps(steps) {
    let newSteps = steps;
    if (!Array.isArray(newSteps)) {
      newSteps = [newSteps];
    }

    if (!newSteps.length) {
      return;
    }

    this.setState(currentState => {
      currentState.steps = currentState.steps.concat(newSteps);
      return currentState;
    });
  }

  callback(data) {
    this.setState({
      selector: data.type === "tooltip:before" ? data.step.selector : ""
    });
  }

  next() {
    this.joyride.next();
  }

  render() {
    const {
      isReady,
      isRunning,
      joyrideOverlay,
      joyrideType,
      selector,
      stepIndex,
      steps
    } = this.state;
    let html;
    if (isReady) {
      html = (
        <div>
          <Joyride
            ref={c => (this.joyride = c)}
            callback={data => this.callback(data)}
            debug={false}
            disableOverlay={selector === ".card-tickets"}
            locale={{
              back: <span>Back</span>,
              close: <span>Close</span>,
              last: <span>Last</span>,
              next: <span>Next</span>,
              skip: <span>Skip</span>
            }}
            run={isRunning}
            showOverlay={joyrideOverlay}
            showSkipButton={true}
            showStepsProgress={true}
            stepIndex={stepIndex}
            steps={steps}
            type={joyrideType}
          />
          <TopNavigation
            username={this.state.username ? this.state.username : ""}
            }
            roles={this.state.roles}
          />
          <main className="imp">
            <SideNavigation
              currentNav={this.props.location.pathname.substring(1)}
              addSteps={steps => this.addSteps(steps)}
              selector={selector}
              next={() => this.next()}
            />
          </main>
        </div>
      );
    }
    return (
      <div>
        {html}
      </div>
    );
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(AgentDashboard);

SideNavigation.js

代码语言:javascript
运行
复制
const sideMenus = [
  {
    menu: "My Property",
    link: "imp/dashboard/my-properties",
    icon: "icon-hotel",
    class: "hotel-selector"
  },
  {
    menu: "My IMP",
    link: "imp/dashboard/my-imps",
    icon: "icon-people-outline",
    class: "imp-selector"
  },
];

class SideNavigation extends React.PureComponent {
  componentDidMount() {
    const steps = [
      {
        title: "Trigger Action",
        text: "It can be click",
        selector: ".imp-dashboard",
        position: "right",
        type: "hover",
      },
      {
        title: "Advance customization",
        text:
          "You can set individual styling options for beacons and tooltips.",
        selector: ".hotel-selector",
        position: "bottom",
        allowClicksThruHole: true,
        style: {
          backgroundColor: "#ccc",
          mainColor: "#000",
          header: {
            color: "#f04",
            fontSize: "3rem",
            textAlign: "center"
          },
          footer: {
            display: "none"
          },
          beacon: {
            inner: "#000",
            outer: "#000"
          }
        }
      }
    ];
    this.props.addSteps(steps);
  }

  handleClick = e => {
    e.preventDefault();
    const { next } = this.props;
    next();
  };

  render() {
    const { currentNav, selector } = this.props;
    const menuToShow = sideMenus.map(menuItem =>
      <Menu.Item key={menuItem.link} className={menuItem.class}>
        <NavLink
          activeClassName={currentNav.includes(menuItem.link) ? "active" : ""}
          to={`/${menuItem.link}`}
        >
          <i className={menuItem.icon} />
          <span>
            {menuItem.menu}
          </span>
        </NavLink>
      </Menu.Item>
    );
    return (
      <Sidebar.Pushable>
        <Sidebar
          as={Menu}
          width="thin"
          visible
          icon="labeled"
          vertical
          inverted
        >
          <Menu.Item name="dashboard" className="imp-dashboard">
            <NavLink
              activeClassName={currentNav === "imp/dashboard" ? "active" : ""}
              to="/imp/dashboard"
            >
              <i className="icon-activity" />
              <span>Dashboard</span>
            </NavLink>
          </Menu.Item>
          {menuToShow}
        </Sidebar>
        <Sidebar.Pusher>
          <Routes />
        </Sidebar.Pusher>
      </Sidebar.Pushable>
    );
  }
}

你认为有可能使用这种组件分解/结构的反应-愉悦吗?我完全不知道该如何使用它。有人能指点我吗?我成功地编写了上面的代码,在这里我没有收到任何错误,也没有看到在单击按钮时任何入门部件都在工作。我错过了什么吗?

为了提高可读性,下面是gist中的代码

https://gist.github.com/anonymous/bc0121ab23652513c2639a0aa55c4390

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-29 13:56:15

是的,如果组件故障(顶部和侧面导航),这是可能的。重要的是要正确地配置"arrayOfSteps“以方便出行。重要的是,要在joyride组件中配置步骤的“选择器”属性。

例如,步骤数组可以遍历顶部和侧面的导航标记。类似于:

代码语言:javascript
运行
复制
   {
  title: 'First Step',
  text: 'Start using the <strong>joyride</strong>',
  selector: '#first-top_menu',
  position: 'bottom-left',
  type: 'hover',
  isFixed: true,
  // optional styling
  style:{
   .....
    }
},
{
  title: 'Second Step',
  text: 'Start using the <strong>joyride</strong>',
  selector: '#second-top_menu',
  position: 'bottom-left',
  type: 'hover',
  isFixed: true,
  // optional styling
  style:{
   .....
    }
},
{
  title: 'Third Step',
  text: 'Start using the <strong>joyride</strong>',
  selector: '#first-side_menu',
  position: 'bottom-left',
  type: 'hover',
  isFixed: true,
  // optional styling
  style:{
   .....
    }
},
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45938314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档