我试图制作一个可以改变颜色并将其值传递给Redux商店的组件,但是我很难实现它。
当我添加一个将颜色更改为onClick事件的函数时,它将起作用。当我添加第二个传递值的函数时,只有第二个函数执行。
我试图将两个功能都放置在另一个功能中,但是没有用。
import React, { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import * as actionTypes from "../../store/Actions";
import { CSSTransition } from "react-transition-group";
import "./Shirts.scss";
import white from "../../assets/white.jpg";
import black from "../../assets/black.jpg";
import grey from "../../assets/grey.jpg";
const shirts = { white, black, grey };
function Shirts() {
const [selected, setSelected] = useState(shirts.black);
const selectedPoster = useSelector(state => state.poster);
const dispatch = useDispatch();
const passColor = e =>
dispatch({ type: actionTypes.PASS_COLOR, payload: e.currentTarget.value });
const changeColor = () => {
setSelected(shirts.white);
};
return (
<div>
<div className="shirts">
<CSSTransition
in={true}
key={shirts}
appear={true}
timeout={300}
classNames="fade"
>
<img alt="T-Shirt" className="shirt" src={selected} />
</CSSTransition>
</div>
<div>
<img className="selected-poster" alt="Poster" src={selectedPoster} />
</div>
<div className="color-picker">
<button
className="white-button"
value="White"
onClick={(changeColor, passColor)}
>
<p className="white-text">
W<br />
H<br />
I<br />
T<br />E
</p>
</button>
<button
className="grey-button"
value="Grey"
onClick={(() => setSelected(shirts.grey), passColor)}
>
<p className="grey-text">
G<br />
R<br />
E<br />Y
</p>
</button>
<button
className="black-button"
value="Black"
onClick={(() => setSelected(shirts.black), passColor)}
>
<p className="black-text">
B<br />
L<br />
A<br />
C<br />K
</p>
</button>
</div>
</div>
);
}
export default Shirts;
答案 0 :(得分:0)
正确的语法是:
onClick={((e) => setSelected(shirts.grey), passColor(e))}