['negative', '', 'positive'][Math.sign(article.total_comments.length) + 1]
onChangePicture = e =>
this.setState(
{ data: { ...this.state.data, [e.target.name]: e.target.files[0] } },
this.onSubmit
);
onSubmit = e => {
if (e) e.preventDefault();
const errors = this.validate(this.state.data);
this.setState({ errors });
if (Object.keys(errors).length === 0) {
this.setState({ loading: true });
this.props.submit(this.state.data);
}
};
import React from 'react';
import classnames from 'classnames';
class SomeComponent extends React.PureComponent {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
this.state = {
show: false,
};
}
onClick() {
this.setState({ show: !this.state.show });
}
render() {
const { show } = this.state;
return <div onClick={this.onClick} className={classnames('some-class', { 'change-class': show })} />;
}
}
export default SomeComponent;
...
componentDidMount() {
const { editorArea } = this.refs;
const editor = new SirTrevor.Editor({
el: editorArea,
defaultType: 'Text',
iconUrl: 'build/sir-trevor-icons.svg'
});
}
render() {
return (
<div>
<textarea className="js-st-instance" ref="editorArea"></textarea>
</div>
)
}
...
Как правильно использовать this.props.children?
import React from 'react';
import ReactDOM from 'react-dom';
const SomeComponent = ({ name }) => (
<span>{name}</span>
);
const SomeContainer = ({ children }) => (
<div>{children}</div>
);
const Example = () => (
<SomeContainer>
<SomeComponent name="John" />
</SomeContainer>
);
ReactDOM.render(
<Example />,
document.getElementById('root');
);
<div id="root">
<div>
<span>John</span>
</div>
</div>