te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Open a popUp in react - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open a popUp in react - Stack Overflow

programmeradmin3浏览0评论

I want to open a popup when I click a button on react, I have this but the popUp wont appear:

 <button type="button" class="btn btn-primary" data-toggle="modal" data-target={`#${props.idMessage}`}>
                {props.idMessage}
            </button>


            <div class="modal fade" id={props.idMessage} tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

Using the react debug tools I can see that the props.id obviously differ from each one and the value on my data-target is the same of my id, as you can see the popUp should appear:

I want to open a popup when I click a button on react, I have this but the popUp wont appear:

 <button type="button" class="btn btn-primary" data-toggle="modal" data-target={`#${props.idMessage}`}>
                {props.idMessage}
            </button>


            <div class="modal fade" id={props.idMessage} tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

Using the react debug tools I can see that the props.id obviously differ from each one and the value on my data-target is the same of my id, as you can see the popUp should appear:

Share Improve this question asked Apr 4, 2019 at 14:59 jose azevedojose azevedo 2452 gold badges3 silver badges20 bronze badges 1
  • hello, i don't know which library you are using but there are lots of react ui libraries out there that are easier to use that instead of using the attributes, we should be using props and state, after all we are in jsx scope not html anymore making logic like this easier to implement. Try this: react-md.mlaursen./ponents/dialogs – Lelouch Commented Apr 4, 2019 at 15:25
Add a ment  | 

3 Answers 3

Reset to default 5

Here is modern way to achieve this using React Hooks

import React, { useState } from "react";

const PopUp = ({ idMessage }) => {
  // create state `open` with default as false
  const [open, setOpen] = useState(false);
  return (
    <>
      {/* click of button toggles `open` value therefore visibility */}
      <button
        onClick={() => setOpen(!open)}
        type="button"
        className="btn btn-primary"
        data-toggle="modal"
        data-target={`#${idMessage}`}
      >
        {idMessage}
      </button>
      {/* If open is true show your <div /> */}
      {open && (
        <div
          className="modal fade"
          id={idMessage}
          tabIndex="-1"
          role="dialog"
          aria-labelledby="exampleModalLabel"
          aria-hidden="true"
        >
          content
        </div>
      )}
    </>
  );
};

export default PopUp;

You could consider creating the popup as a re-usable ponent, that just renders the props.message. Suppose you have the button in App.js, here is how you can add the click listener on it.

class App extends Component {
  state = {showPopup: false};

  openPopupHandler = () => {
    this.setState({showPopup: true});
  }
  
  closePopupHandler = () => {
    this.setState({showPopup: false});
  }

  
  render() {
    let popup = null;
    if(this.state.showPopup) {
      popup = (<Popup message='the text you need to display' closeMe={this.closePopupHandler}/>);
    } 
    return(
      <div>
      <button onClick={this.clicked}>Click me </button>
      {popup}
      </div>
    );
  }
}

And you can define the popup ponent as given below.

Popup.js

const popup = (props) => {
return (
  <div>
    <p>{props.message}</p>
    <button onClick={props.closeMe}>Close Popup</button>
  </div>
);
}

Also, style the popup ponent with the size as per your requirement and with an z-index greater than that of the parent ponent.

How about this :

class PopUp extends Component {
 constructor(props) {
    super(props);
    this.state = {
        hide: false
    };
 }
 clicked(){
     this.setState({
       hide: true
     });
 }
 render() {
     return (
        <div>
        <button type="button" class="btn btn-primary" data-toggle="modal" onClick={() => this.clicked()} >
            Click Me
        </button>
        {
          this.state.hide? 
              <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">Required PopUp </div>
              : null
        }
        </div>
     );
  }
}

you can use this as <PopUp> in your code

发布评论

评论列表(0)

  1. 暂无评论