const obj = {
'0': 'some value 0',
'1': 'some value 1',
'2': false,
length: 3,
};
const filteredArr = Array.prototype.filter.call(obj, el => el);
console.log(filteredArr); // ["0 value", "1 value"]
function Slider(props) {
/* ... */
this.images = props.images;
this.currentSlide = 1;
/* ... */
}
Slider.prototype.changeSlide = function() {
/* ... */
if (this.currentSlide === this.images.length) {
currentSlide = 1;
} else {
this.currentSlide++;
}
/* ... */
}
class Parent extends Component {
state = {
search: '',
data: [],
};
componentDidMount() {
fetchData().then(({ data }) => this.setState({ data }));
}
handleFilterChange = e => {
this.setState({ search: e.target.value });
};
render() {
const { data, search } = this.state;
const filteredData = data.filter(el => /* some stuff */);
return (
<Wrapper>
<SearchFilter onChange={this.handleFilterChange} />
<DataList data={filteredData} />
</Wrapper>
)
}
}
сlick(e, item) {
console.log(item);
}
render() {
const { array } = this.state;
const unit = array.map((item, index) => (
<a
key={index}
style={{display:"block"}}
onClick={(e) => this.click(e, item)}
>
Hello, {item}
</a>
));
label={this.props._sharedData.loc.advanced_search.select_brand}
isValidEmail("mail@gmail.com").then(result => someAction(result));
async someHandler() {
const result = await isValidEmail("mail@gmail.com");
return someAction(result);
}
async someOtherHandler() {
const result = await isValidEmail("mail@gmail.com");
/* вызов someOtherAsyncAction тоже возвращает Promise, поэтому ждем
и возвращаем только когда придет результат, для этого используем
ключевое слово await */
return await someOtherAsyncAction(result);
}
<li className={someCondition ? someClass : someOtherClass} />
someProp === SOME_CONSTRANT
<li className={getClassName(someProp)} />
key = {this.props.data[index]['id']}
key = {value.id}
onChange = value => {
const [from, to] = value;
const filteredStudios = this.state.studios.filter(studio =>
studio.price >= from && studio.price <= to
);
this.setState({
studios: filteredStudios,
from,
to,
});
};
const allCars = {
bmw: [
{ name: 'x1', ... },
...
],
...
}
render() {
const { mark } = this.props;
const models = allCars[mark];
return (
<div>
{mark && (
<div>
<label>{mark}</label>
{models.map(model => (
<div key={model.name}>
<label>{model.name}</label>
<input
name="model"
value={model.name}
type="checkbox"
onClick={this.props.changemodel}
/>
</div>
))}
<div>
)}
</div>
);
}
changeMark (e) {
const { value } = e.target;
this.setState({
mark: value === 'all' ? [] : [...prevState.mark, value],
model: [],
page: 1,
});
}
}
handleCheckboxCheck(e) {
const { name, value } = e.target;
this.setState(prevState => ({
[name]: _.xor(prevState[name], +value),
}));
}
constructor(props) {
super(props);
this.changeModel = this.changeModel.bind(this);
}
changeModel (e) {
// some actions
}
changeModel = e => {
// some actions
};
class myClass {
createSomeObj() {
const newObj = { 1: 1, 2: 2 };
newObj.__proto__ = this.__proto__;
return newObj;
}
}
const instance = new myClass();
const instanceOfInstance = instance.createSomeObj();
console.log(instanceOfInstance.createSomeObj()); // myClass {1: 1, 2: 2}
class myClass {
createSomeObj() {
const newObj = { 1: 1, 2: 2 };
newObj.__proto__.constructor = myClass;
return newObj;
}
}
const instance = new myClass();
console.log(instance.createSomeObj()); // myClass {1: 1, 2: 2}
class myClass {
constructor(props) {
Object.keys(props).forEach(key => this[key] = props[key]);
}
static createSomeObj() {
return new myClass({ 1: 1, 2: 2 });
}
}
console.log(myClass.createSomeObj()); // myClass {1: 1, 2: 2}