const store1 = configureStore1();
const store2 = configureStore2();
const App = () => (
<>
<Provider store={store1}>
<MainProject />
</Provider>
<Provider store={store2}>
<SubProject />
</Provider>
</>
);
const store1 = configureStore1();
const store2 = configureStore2();
const App = () => (
<Provider store={store1}>
<>
<MainProject />
<Provider store={store2}>
<SubProject />
</Provider>
</>
</Provider>
);
function Y({ x }) {
const [y, setY] = useState(x);
useEffect(() => {
if (x !== y) {
setY(x);
}
}, [x, y]);
return ( ... );
}
const TabContent = ({ content }) => (
<div className="accordion">
<p>{content}</p>
</div>
);
function Tabs({ items }) {
/* (1) Добавлено значение по-умолчанию для активной вкладки */
const [active, setActive] = React.useState(0);
/* (2) Хандлер по-хорошему не должен ничего возвращать */
const openTab = e => {
setActive(+e.target.dataset.index);
};
return (
<div>
<div className="tab">
{items.map((n, i) => (
<button
/* (3) Добавлено свойство key */
key={i}
className={`tablinks${i === active ? ' active' : ''}`}
onClick={openTab}
data-index={i}
>
{n.title}
</button>
))}
</div>
{items[active] && <TabContent {...items[active]} />}
</div>
);
}
/* (4) Добавлено свойство title для каждого пункта */
/* (5) Добавлены открывающие кавычки для значений content */
const items = [
{
title: 'First',
content:
'1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
},
{
title: 'Second',
content:
'2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
},
{
title: 'Third',
content:
'3. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
}
];
ReactDOM.render(<Tabs items={items} />, document.getElementById('app'));
import React, { Component } from "react";
class Filters extends Component {
state = {
form: [
{ label: "Все", name: "all", value: -1 },
{ label: "Яблоко", name: "apple", value: 0, checked: true },
{ label: "Груша", name: "pear", value: 1, checked: true },
{ label: "Арбуз", name: "watermelon", value: 2, checked: true },
{ label: "Абрикос", name: "apricot", value: 3, checked: true }
]
};
onHandleChange = e => {
const { checked, name } = e.target;
const { form } = this.state;
const index = form.findIndex(item => item.name === name);
const item = form[index];
const newForm = [...form];
newForm[index] = { ...item, checked };
this.setState({ form: newForm });
};
render() {
return (
<div className="filters">
<div className="stops-quantity">
<p className="currency__name">Выбрать</p>
{this.state.form.map(item => (
<div className="check" key={item.name}>
<input
className="stops__checked"
type="checkbox"
name={item.name}
value={item.value}
checked={item.checked}
onChange={this.onHandleChange}
/>
<label className="stops__label" htmlFor="check1">
{item.label}
</label>
</div>
))}
</div>
</div>
);
}
}
export default Filters;
По какому принципу взаимодействия с сервером работает React?
Т.е есть запущенный Реакт, и этот реакт сам по себе там работает и обменивается с сервером данными через ajax запросы?
Т.е React работает на npm
Не могу точнее объяснить ибо не особо знаю реакт.
const handler = (e) => {
const { target } = e;
const value = target.type === 'checkbox' ? target.checked : target.value;
const { name } = target;
setForm( f => ({ ...f, [name]: value }));
};
const handler = () => {
setX(!x);
};
const handler = useCallback(() => {
setX(!x);
}, []);
import React, { Fragment, useCallback, useState } from "react";
import ReactDOM from "react-dom";
const arrProd = [
{
price: "5.75",
sold: "100"
},
{
price: "2.36",
sold: "15"
},
{
price: "5.48",
sold: "20"
},
{
price: "4.49",
sold: "200"
},
{
price: "3.15",
sold: "258"
},
{
price: "9.99",
sold: "479"
},
{
price: "4.8",
sold: "147"
},
{
price: "8",
sold: "951"
},
{
price: "4.27",
sold: "753"
},
{
price: "2.46",
sold: "852"
},
{
price: "4.99",
sold: "742"
},
{
price: "3.49",
sold: "10"
},
{
price: "2",
sold: "26"
},
{
price: "3.83",
sold: "39"
},
{
price: "9.98",
sold: "47"
},
{
price: "6.77",
sold: "96"
}
];
function App() {
const [form, setForm] = useState({
minPrice: "",
maxPrice: "",
sold: ""
});
const { minPrice, maxPrice, sold } = form;
const [products, setProducts] = useState(arrProd);
const handleChange = useCallback(e => {
const { name, value } = e.target;
setForm(f => ({ ...f, [name]: value }));
}, []);
const handleSubmit = useCallback(
e => {
e.preventDefault();
const filteredProducts = arrProd.filter(
product =>
(!maxPrice || +maxPrice >= +product.price) &&
(!minPrice || +minPrice <= +product.price) &&
(!sold || +sold >= +product.sold)
);
setProducts(filteredProducts);
},
[minPrice, maxPrice, sold]
);
const isPriceRangeValid = !minPrice || !maxPrice || +minPrice <= +maxPrice;
const isSoldValid = +sold >= 0;
return (
<Fragment>
<form onSubmit={handleSubmit}>
{!isPriceRangeValid && <div>"Please provide a valid price range"</div>}
Min
<input
type="text"
name="minPrice"
size={6}
maxLength={10}
value={minPrice}
onChange={handleChange}
/>
Max
<input
type="text"
name="maxPrice"
size={6}
maxLength={10}
value={maxPrice}
onChange={handleChange}
/>
{!isSoldValid && <div>Please provide a valid sold value</div>}
Sold
<input
type="text"
name="sold"
size={6}
maxLength={10}
value={sold}
onChange={handleChange}
/>
<input type="submit" title={"Submit price range"} value={"Go"} />
</form>
<ul>
{products.map((product, i) => (
<li key={product.sold + i}>
{product.price} - {product.sold}
</li>
))}
</ul>
</Fragment>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);