- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我继承了一个react/node/prismic应用程序,我们需要更新prismic-reactjs包,这导致需要更新其他一些包——总的来说,我们改变了:
prismic-reactjs:0.2.0 → 1.1.0
react :15.6.1 → 16.0.0
webpack:3.12.0 → 4.39.2
react-dom:15.6.1 → 16.0.0
react-router-dom:4.1.2 → 5.0.1
extract-text-webpack-plugin(已弃用)→ mini-css-extract-plugin
然后删除了“withRouter()”的一个使用,因为启动本地服务器时出现了新错误(但我在另一个分支中确认,单独进行该编辑不会产生下面的症状)
上下文:我们有一个 personMap,它显示一组人的图标,您单击其中一个即可打开 PersonMapStory 模式,该模式显示该人的故事,然后链接到该人的其他人底部的列表。如果您向下滚动并单击其中一个链接,顶部的故事将相应替换(用新人的故事),下面列出的是其他所有人。在代码中,这些底部链接的按钮 onClick 行为是 setActivePerson() 给这个新人。
新错误:在这个新分支中,当我们单击底部链接时,它会为#2 调用 setActivePerson(),但随后也会再次为 #1 调用 setActivePerson()! (从原始 PersonMap 里的 onClick 行为返回)。因此,症状是您看到第一个故事,向下滚动并单击另一个人的链接,但模式似乎根本没有更新(尽管它实际上更新了两次)。在当前/生产/工作分支中,它只更新一次给新人,仅此而已。这种新行为听起来确实类似于 this onClick 传播问题。我尝试将 stopPropagation() 添加到适当的方法中,但没有成功。
更多信息:在这个分支中,看起来来自该模式内部的任何点击(即使我点击顶部显示的故事)都会调用原始人的 SetActivePerson() ,来自当 thisPersonIsQueued 为 true 时,PersonMap 订单项 onClick 行为。 (因此,如果我向下滚动并单击一个新人,我们会 SetActivePerson person #2,然后立即 SetActivePerson person #1,因此模式会保持相同的故事并且永远不会更新)。在 PersonMap 状态(在 Chrome 开发者工具/组件中查看)中,有一个 queuedUpPatient:通常在这种情况下(在原始/生产代码中)queuedUpPatient 为 null,但在这个新分支中,queuedUpPatient 保留 Person #1 的值。 (或者已清除并重新设置)。
调试器中可能的线索:在我们为 #2 调用 SetActivePerson 之后(这是我们常规/生产分支中发生的最后一步),在我们看到对 person #1 的 setActivePerson 调用之前它是 onClick 行为,调试器引导我完成react-dom.development.js 中的几个方法,从 callCallback() 的最后一行开始,在以下情况的内部代码中(见下文):
// Check that the browser supports the APIs we need to implement our special
// DEV version of invokeGuardedCallback
if (typeof window !== 'undefined' && typeof window.dispatchEvent === 'function' && typeof document
关于可能发生的情况以及如何使其再次正常工作以便我们可以继续进行软件包升级的其他想法?这是 map 和模态的两个相关文件(如下)。
PersonMap.js:
import Modal from 'react-modal'
import PropTypes from 'prop-types'
import React from 'react'
import PersonMapPoint from './PersonMapPoint'
import PersonMapStory from './PersonMapStory'
import PersonMapCallout from './PersonMapCallout'
import PersonMapLocator from './PersonMapLocator'
import PersonMapBackground from './PersonMapBackground'
const CUSTOM_STYLES = {
content: {
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: '#fff',
zIndex: 10,
border: 'none'
}
}
class PersonMap extends React.Component {
constructor(props) {
super(props)
this.setActivePerson = this.setActivePerson.bind(this)
this.setNoActivePerson = this.setNoActivePerson.bind(this)
this.setQueuedUpPerson = this.setQueuedUpPerson.bind(this)
this.setNoQueuedUpPerson = this.setNoQueuedUpPerson.bind(this)
this.checkBlurEvent = this.checkBlurEvent.bind(this)
this.setIsDesktop = this.setIsDesktop.bind(this)
this.checkHasBeenScrolledTo = this.checkHasBeenScrolledTo.bind(this)
this.setTopRef = this.setTopRef.bind(this)
this.handleKeyPress = this.handleKeyPress.bind(this)
this.state = {
activePerson: null,
queuedUpPerson: null,
scrollPos: 0,
isDesktop: false,
hasBeenScrolledTo: false,
lastQueuedPerson: null
}
}
componentDidMount() {
this.setIsDesktop()
this.checkHasBeenScrolledTo()
window.addEventListener('resize', this.setIsDesktop)
window.addEventListener('scroll', this.checkHasBeenScrolledTo)
}
componentWillUnmount() {
window.removeEventListener('resize', this.setIsDesktop)
window.removeEventListener('scroll', this.checkHasBeenScrolledTo)
}
setTopRef(element) {
this.topRef = element
}
setActivePerson(personName) {
this.setState({
activePerson: personName,
scrollPos: window.scrollY
})
event.stopPropagation()
}
setNoActivePerson() {
this.setState({
queuedUpPerson: this.state.activePerson,
activePerson: null
}, () => {
setTimeout(() => {
window.scrollTo(0, this.state.scrollPos)
}, 50)
})
}
setQueuedUpPerson(personName) {
this.setState({
queuedUpPerson: personName,
lastQueuedPerson: personName
})
}
setNoQueuedUpPerson() {
this.setState({
queuedUpPerson: null
})
event.stopPropagation()
}
handleKeyPress(e, name) {
if (e.key !== ' ' && e.key !== 'Enter') {
return
}
this.setActivePerson(name)
}
checkBlurEvent(e) {
if (Array.from(e.currentTarget.childNodes[0].childNodes).includes(e.relatedTarget)) {
return
}
this.setNoQueuedUpPerson()
}
render() {
return (
<section className="slice-area person-map CONSTRAIN">
<div className="person-map-headers">
<div className="person-map-headers-inner">
<h1 className="person-map-title">
{this.props.title}
</h1>
<p className="person-map-disclaimer">
{this.props.disclaimer}
</p>
</div>
</div>
<div
className={`person-map-list-wrap${ this.state.isDesktop ? ' --desktop' : '' }`}
ref={this.state.isDesktop && this.setTopRef}
>
{ this.state.isDesktop &&
<PersonMapBackground isVisible={this.state.hasBeenScrolledTo}/>
}
<ul className="person-map-list">
{
this.props.personStories.map((person) => {
const thisPersonIsQueued = this.state.queuedUpPerson === person.name
const thisPersonIsActive = this.state.activePerson === person.name
const thisPersonWasLastQueued = this.state.lastQueuedPerson === person.name
return (
<li
key={person.name} className={`person-map-list-item${thisPersonWasLastQueued ? ' --active' : ''}`}
onMouseEnter={this.state.isDesktop ? () => this.setQueuedUpPerson(person.name) : null}
onMouseLeave={this.state.isDesktop ? this.setNoQueuedUpPerson : null}
onFocus={this.state.isDesktop ? () => this.setQueuedUpPerson(person.name) : null}
onBlur={this.state.isDesktop ? this.checkBlurEvent : null}
onClick={thisPersonIsQueued || !this.state.isDesktop ? () => this.setActivePerson(person.name) : null}
onKeyPress={(e) => this.handleKeyPress(e, person.name)}
>
{
<PersonMapLocator
x={person.x}
y={person.y}
>
}
<Modal
isOpen={thisPersonIsActive}
onRequestClose={this.setNoActivePerson}
style={CUSTOM_STYLES}
>
<PersonMapStory
name={person.name}
photo={person.photo_url}
story={person.story}
setNoActivePerson={this.setNoActivePerson}
setActivePerson={this.setActivePerson}
isActive={thisPersonIsActive}
otherPersons={this.props.personStories.filter((item) => item.name !== person.name).map((item) => ({ name: item.name, photo: item.photo_url }))}
isDesktop={this.state.isDesktop}
/>
</Modal>
</li>
)
})
}
</ul>
</div>
</section>
)
}
}
PersonMap.propTypes = {
title: PropTypes.string,
disclaimer: PropTypes.string,
personStories: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
photo_url: PropTypes.string,
x: PropTypes.number,
y: PropTypes.number,
story: PropTypes.shape({
title: PropTypes.string,
link: PropTypes.string,
content: PropTypes.arrayOf(PropTypes.shape({
question: PropTypes.string,
answer: PropTypes.string
}))
})
}))
}
export default PersonMap
PersonMapStory.js:
import PropTypes from 'prop-types'
import React from 'react'
import PersonMapPerson from './PersonMapPerson'
class PersonMapStory extends React.Component {
constructor(props) {
console.log("PersonMapStory constructor")
super(props)
this.deactivateThisPerson = this.deactivateThisPerson.bind(this)
this.check = 0
}
deactivateThisPerson(backPressed = false) {
if (!backPressed) {
history.back()
}
console.log("Set no active person ")
this.props.setNoActivePerson()
}
setActivePerson(name) {
this.props.setActivePerson(name)
event.stopPropagation()
}
componentDidMount() {
const loc = window.location.pathname.substr(1)
this.setState({ location: loc })
const openState = { modalOpen: true }
history.pushState(openState, 'signup-open', loc)
}
render() {
return (
<div className={`person-map-story${this.props.isActive ? ' --active' : ''}${this.props.isDesktop ? ' --desktop' : ''}`}>
<h2 className="person-map-story-title">
{ this.props.story.title }
</h2>
<button className="person-map-story-close-button" onClick={() => {this.deactivateThisPerson(false)}}>
<span className="person-map-story-close-button-text">
Close Story
</span>
</button>
<div className="person-map-story-body">
<span className="person-map-story-person-wrap">
<PersonMapPerson
photo={this.props.photo}
name={this.props.name}
/>
</span>
{
this.props.story.content.map((section) => {
if (section) {
return (
<div key={section.question}>
<h3 className="person-map-story-question">{section.question}</h3>
<p className="person-map-story-answer">
{section.answer}
</p>
</div>
)
}
})
}
{
this.props.story.link &&
<a href={this.props.story.link}
target="_blank"
className="person-map-story-more-link header-and-text-link"
> Read More Stories
</a>
}
<ul className="person-map-story-other-list">
{
this.props.otherPersons.map((person) => (
<li
key={person.name}
className="person-map-story-other-list-item"
>
<button className="person-map-story-other-button" onClick={() => this.setActivePerson(person.name)}>
<PersonMapPerson
name={person.name}
photo={person.photo}
ctaText="View their story"
/>
</button>
</li>
))
}
</ul>
</div>
</div>
)
}
}
PersonMapStory.propTypes = {
name: PropTypes.string,
photo: PropTypes.string,
story: PropTypes.shape({
title: PropTypes.string,
link: PropTypes.string,
content: PropTypes.arrayOf(PropTypes.shape({
question: PropTypes.string,
answer: PropTypes.string
}))
}),
setNoActivePerson: PropTypes.func,
setActivePerson: PropTypes.func,
isActive: PropTypes.bool,
otherPersons: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string,
photo: PropTypes.string
})),
isDesktop: PropTypes.bool
}
export default PersonMapStory
最佳答案
更新 PersonMapStory
中的 onClick
以访问 event
对象:
<button className="person-map-story-other-button" onClick={e => this.setActivePerson(e, person.name)}>
并更改 PersonMapStory
中的 setActivePerson(name)
函数:
setActivePerson(event, name) {
this.props.setActivePerson(name)
event.stopPropagation()
}
关于javascript - 自从更新react-dom和react-router-dom包: excessive li onClick behavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58530473/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 6 年前。 Improve
Polymer的light DOM和local DOM有什么区别?来自文档(1): The DOM that an element creates and manages is called its
当内容添加到网页时,我需要触发一个 Action 。更新可能具有不同的性质(例如 AJAX、延迟脚本、用户操作)并且不受我的控制。 我想使用 DOM 突变事件,但它们并非在所有浏览器中都可用。是否有为
我刚遇到一个有趣的情况,我有一个提交 放置在 内的 native 自定义元素的 Shadow DOM 内. Select #shadow-root ...
假设有一个滚动列表,当我插入一些新的 DOM 附加到当前 dom 时,它工作正常。上拉 但是如果我之前插入一些新的 DOM,新的 DOM 将在视口(viewport)中,而旧的 DOM 将被下推。下拉
在我的项目中实现 Shadow DOM 是否会使它们像 React 使用的虚拟 DOM 一样更快? 最佳答案 它们是不同用途的不同事物,因此比较性能没有意义。 虚拟 DOM 虚拟 DOM 旨在避免对
在我的页面内容上,我将多张卡片组织成网格 __________________ | ____ ____ | | | | | | | | | | | |
是否可以在浏览器中看到(调试)从 DOM 元素触发的自定义事件? 假设我想查看 Bootstrap Collapse 的哪个特定元素触发了 show.bs.collapse event ,我能以某种方
我正在生成用于客户端的 XPaths 服务器端,我很困惑为什么在 DOM 中找不到表路径(即 td 中的内容)。 事实证明,现代浏览器(至少是 Chrome 和 Firefox)插入了 tbody在文
是否可以检索文本节点的几何位置(即从父元素、页面等的顶部/左侧偏移量)? 最佳答案 不是直接的。 TextNode 没有用于测量视口(viewport)定位的原始 IE 偏移*(和类似的)扩展。 仅在
以下语句中的 DOM 元素的含义是什么? Statement #1 You can add multiple classes to a single DOM element. Statement #2
有没有办法让 firebug(或任何其他浏览器,或使用任何其他工具)阻止任何 dom 操作的发生?有时布局调试充满悬停事件的屏幕是不可能的,因为元素可能会消失,并且您看不到它们的复合布局。 最佳答案
我需要在html文档中搜索 text here 然后输出完整的节点路径(CSS或XPATH) 例如 html > body > div class ="something" > table > tr
这是我的一个页面的典型加载时间如何拆分为:- Domain Lookup 0 0 % Connect 134 .3% Request
我的 .on() 工作时遇到一些问题。我的网站是here . 如果你看看 www.eliteweb-creation.co.uk/dev/js/nav.js,我正在 mouseenter 和 mous
我是 Javascript 的新手,负责将我们产品的 UI 从 YUI2 迁移到 YUI3。看起来哪里都没有迁移指南,所以我现在正在浏览互联网帖子和 yui 文档。 在我的全局范围内,我临时添加了类似
我想和实习生一起测试一些 DOM 相关的东西,不需要特定的固定装置,只是一般的 DOM 东西,比如我改变了 Element.prototype。这是否需要通过本地 Selenium 服务器(或 sau
我是 HTML 和 HTML5 的初学者。 当我阅读以下内容时 link ,我找到了术语 DOM 和 DOM API。我通读了维基百科,但无法理解其背后的全部思想。 谁能给我解释一下: 文档对象模型
我有两个主要问题。 Object 之类的扩展是否算数? 什么是 DOM 包装? http://perfectionkills.com/whats-wrong-with-extending-the-do
对不起查询,原型(prototype),雅虎 YUI,道场在考虑小的时候不吸引我。我想要一个模块化的库,代码尽可能小,最多 20Kb [un compressed] 是我所期望的。应该提供 Dom 操
我是一名优秀的程序员,十分优秀!