кликая на один список открывается и второй
The functions passed to thereducers
parameter can be accessed through thecaseReducers
return field.
playlistSlice.caseReducers.playAudio(state, { payload: state.id });
Объект менять не нужно.
const SERIES = [
{
comment: '1 сезон',
folder: [
{ comment: '1 серия' },
{ comment: '2 серия' },
],
},
{
comment: '2 сезон',
folder: [
{ comment: '1 серия' },
{ comment: '2 серия' },
{ comment: '3 серия' },
],
},
];
const ItemsList = ({ header, items, active, onChange }) => (
<div>
<h3>{header}</h3>
<ul className="items">
{items.map(({ comment }, i) => (
<li
className={`item ${active === i ? 'active' : ''}`}
onClick={() => onChange?.(i !== active ? i : -1)}
>
{comment}
</li>
))}
</ul>
</div>
);
function App() {
const [ iSeason, setActiveSeason ] = useState(-1);
const [ iEpisode, setActiveEpisode ] = useState(-1);
const season = SERIES[iSeason];
const episode = season?.folder[iEpisode];
useEffect(() => {
setActiveEpisode(-1);
}, [ iSeason ]);
return (
<div>
<ItemsList
header="сезоны"
items={SERIES}
active={iSeason}
onChange={setActiveSeason}
/>
{season && <ItemsList
header="серии"
active={iEpisode}
onChange={setActiveEpisode}
items={season.folder}
/>}
{episode && <div>Выбрали: {season.comment}, {episode.comment}</div>}
</div>
);
}
case ADD_PRODUCT:
const item = state.products.find(n => n.id === action.payload.id);
return {
...state,
products: item
? state.products.map(n => n === item ? { ...n, count: n.count + 1 } : n)
: [ ...state.products, { ...action.payload, count: 1 } ],
};
Пушу в массив айтем и хочу затем проверять, если он в массиве уже есть, то...
return {
...state,
items: state.items.includes(action.payload)
? state.items.filter(n => n !== action.payload)
: [ ...state.items, action.payload ],
};
const index = state.items.indexOf(action.payload);
if (index === -1) {
state.items.push(action.payload);
} else {
state.items.splice(index, 1);
}
return {
...state,
array: state.array.map(n => n.id === action.newObject.id ? action.newObject : n),
};
return { ...user, isFollowed: !this.isFollowed };
return {
...state,
plans: state.plans.map(n => {
const item = n.find(m => m.id === action.id);
return item
? n.map(m => m === item ? { ...m, value: action.value } : m)
: n;
}),
};
case 'BASKET':
return {
...state,
products: state.products.map(n => n.name === action.payload
? { ...n, basket: true }
: n
),
};
onPress={() => this.props.addToBasket.bind(this, this.props.product.name)}
onPress={() => this.props.addToBasket(this.props.product.name)}
const showProductsList = (type, maxlength) => { <...> if(product.basket == type & currentLength < maxlength) {
showProductsList("true", 4)
true
не будет равно "true"
, убираем кавычки: showProductsList(true, 4)
.state.products
, вы передаёте в addBasket
его name
. Исправьте вызов addBasket
; если же там всё так, как и задумано, замените прямое обращение по имени свойства (state.products[action.payload]
) на поиск нужного объекта по его name
:Object.values(state.products).find(n => n.name === action.payload)
state.checkboxes.map(n => n.id === action.id
? { ...n, active: !n.active }
: n
)
пишет что state is equal
basketItems: state.basketItems.sort((a, b) => a.price - b.price),
return {
...state,
basketItems: state.basketItems.map(n => n.id === action.id
? { ...n, clicked: !n.clicked }
: n
),
};
values: Array(height).fill(Array(width).fill(null))
values: Array.from({ length: height }, () => Array(width).fill(null))
const initialState = {
player: 'X',
cells: Array.from({ length: 3 }, () => Array(3).fill(null)),
};
const store = createStore((state = initialState, action) => {
switch (action.type) {
case 'MOVE':
return {
...state,
player: state.player === 'X' ? 'O' : 'X',
cells: state.cells.map((row, iRow) => iRow === action.location[0]
? row.map((cell, iCol) => iCol === action.location[1] ? state.player : cell)
: row
),
};
case 'RESET':
return initialState;
default:
return state;
}
});
const Board = connect(({ cells }) => ({ cells }))(({ cells }) => (
<div class="board">
{cells.map((row, iRow) =>
<div className="row">
{row.map((cell, iCol) =>
<Cell location={[ iRow, iCol ]} value={cell} />
)}
</div>
)}
</div>
));
const Cell = connect(null, (dispatch, { location }) => ({
onClick: () => dispatch({ type: 'MOVE', location }),
}))(({ value, onClick }) => (
<div className="cell">
<button onClick={onClick} disabled={value !== null}>
{value}
</button>
</div>
));
const Reset = connect(null, dispatch => ({
reset: () => dispatch({ type: 'RESET' }),
}))(({ reset }) => (
<button onClick={reset}>RESET</button>
));
const App = () => (
<Provider store={store}>
<Reset />
<Board />
</Provider>
);
хочу, чтобы в переменной theme лежало значение state.theme, но по факту получается так: theme.theme
export const theme = (state = 'light', action) => {
switch (action.type) {
case constants.CHANGE_THEME:
return action.payload;
default:
return state;
}
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);