Всем доброго вечера. Изучаю react. 2 примера кода.
1) Здесь функции вынес в compose что бы не использовать в компоненте стрелочные функции или .bind(this)
import React from 'react';
import {compose, withState, withHandlers}from 'recompose';
import PropTypes from 'prop-types';
const DropDown = ({isOpened, onClick, onMouseOver, onMouseOut}) => {
let dropDownText;
if (isOpened){
dropDownText = <div>Here is what is shown is dropdown</div>;
}
return (
<div
onClick={onClick}
onMouseOut={onMouseOut}
onMouseOver={onMouseOver}
>
its drop down bebe
{dropDownText}
</div>
);
};
DropDown.propTypes = {
isOpened: PropTypes.bool,
onClick: PropTypes.func,
onMouseOut: PropTypes.func,
onMouseOver: PropTypes.func,
};
export default compose(
withState('isOpened', 'toggleVisible', false),
withHandlers({
onClick: ({toggleVisible}) => () => {
toggleVisible(isOpened => {
if (isOpened) {
isOpened = false;
}else {
isOpened = true;
}
return isOpened;
});
},
onMouseOver: ({toggleVisible}) => () => {
toggleVisible(isOpened => {
isOpened = true;
return isOpened;
});
},
onMouseOut: ({toggleVisible}) => () => {
toggleVisible(isOpened => {
isOpened = false;
return isOpened;
});
},
})
)(DropDown);
2) Здесь изучаю что такое react-redux.
import React, {Component}from 'react';
import PropTypes from 'prop-types';
import {connect}from 'react-redux';
class App extends Component {
static PropTypes = {
testStore: PropTypes.array.isRequired,
onAddTrack: PropTypes.func,
};
addTrack() {
const props = this.props;
console.log('addTrack', this.trackInput.value);
props.onAddTrack(this.trackInput.value);
this.trackInput.value = '';
}
render() {
const props = this.props;
console.log('this.props', props.testStore);
return (
<div className='container'>
<input ref={input => {this.trackInput = input;}} type='text' />
<button onClick={this.addTrack.bind(this)}>Add Track</button>
<ul>
{
props.testStore.map((track, index) =>
<li key={index}>{track}</li>
)
}
</ul>
</div>
);
}
}
export default connect(
state => ({
testStore: state,
}),
dispatch => ({
onAddTrack: trackName => {
dispatch({type: 'ADD_TRACK', payload: trackName});
},
}),
)(App);
Вопрос, как во втором примере кода вынести функции в compose?