Skip to content

Instantly share code, notes, and snippets.

@dra1n
Created August 2, 2018 07:15
Show Gist options
  • Select an option

  • Save dra1n/13b11fadd6ca86c3d85cd9749bee1b96 to your computer and use it in GitHub Desktop.

Select an option

Save dra1n/13b11fadd6ca86c3d85cd9749bee1b96 to your computer and use it in GitHub Desktop.
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
class Parent extends PureComponent {
handleUserClick(user) {
alert(`Hello I am ${user.name}`)
}
render() { return null }
}
class Child extends Parent {
static propTypes = {
handleUserClick: PropTypes.func.isRequired
}
constructor(props) {
this.state = {
users: props.users,
flag: false
}
}
componentWillMount() {
const { filterClientInput } = this.refs
const { users } = this.state
window.addEventListener('scroll', (e)=>console.log(e.target))
filterClientInput.focus()
if (users.length > 0) {
this.setState(Object.assign({ flag: true }, this.state))
}
}
handleChangeSearchInput(e) {
console.log(e.target.value)
}
render() {
const { handleUserClick } = this
const { users } = this.state
return (
{
flag &&
<div className="page-wrapper">
<input
name={'search-input'}
ref="filterClientInput"
onChange={this.handleChangeSearchInput(e)}
/>
<div>
{
users.map(user =>
<div
className="user-wrapper"
onClick={handleUserClick(user)}
>
<div className="user-name">
{user.name}
</div>
<div className="user-photo">
{user.photo}
</div>
</div>
)
}
</div>
</div>
}
)
}
}
const mapStateToProps = state => ({ users: state.users })
export default connect()(Child)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment