const MainContainer = () => {
const muuriRef = useRef();
// Items state
const [items_ig, setItems_ig] = useState(generateInstaContainer());
const [items_fb, setItems_fb] = useState(generateFbContainer());
const [items_twit, setItems_twit] = useState(generateTwitterContainer());
// Items as children
const children_ig = items_ig.map(({id, color, title, width, height}) => (
<Item_IG
id={id}
key={id}
color={color}
title={title}
width={width}
height={height}
remove={removeFunction_ig}
/>
));
const children_fb = items_fb.map(({id, color, title, width, height}) => (
<Item_FB
id={id}
key={id}
color={color}
title={title}
width={width}
height={height}
remove={removeFunction_fb}
/>
));
const children_twit = items_twit.map(({id, color, title, width, height}) => (
<Item_TWIT
id={id}
key={id}
color={color}
title={title}
width={width}
height={height}
remove={removeFunction_twit}
/>
));
function addInstaGram() {
setItems_ig(items_ig.concat(generateInstaContainer(1)));
}
function addFacebook() {
setItems_fb(items_fb.concat(generateFbContainer(1)));
}
function addTwitter() {
setItems_twit(items_twit.concat(generateTwitterContainer(1)));
}
// render
return (
<MuuriComponent
options={{
layoutDuration: 400,
layoutEasing: "ease",
dragEnabled: true,
dragSortInterval: 50,
dragReleaseDuration: 400,
dragReleseEasing: "ease"
}}
ref={muuriRef}
>
{children_ig}
{children_fb}
{children_twit}
</MuuriComponent>
<div className="test"><Button
onClickF={() => addFacebook()}
onClickT={() => addTwitter()}
onClickI={() => addInstaGram()}
/></div>
);
};
};
我想在另一个组件中使用onClick
操作,但是我不确定如何访问将更新该组件中的网格的功能。我考虑过一个回调函数,但是当我将此组件和新创建的回调发送到主索引时,按钮div被渲染两次,因为它是在第一个实例中返回的。我仍在学习React,主要是熟悉Python。任何帮助表示感谢,谢谢!
这就是我使用的回调函数。
const Button = ({onClickI, onClickT, onClickF}) => {
return ( <div className={someStyles}>
<button onClick={onClickI}>Test 1</button>
<button onClick={onClickT}>Test 3</button>
<button onClick={onClickF}>Test 2</button>
</div> )
答案 0 :(得分:1)
你能试试吗
const Button = ({onClickI, onClickT, onClickF}) => {
return ( <div className={someStyles}>
<button onClick={() => onClickI()}>Test 1</button>
<button onClick={() => onClickT()}>Test 3</button>
<button onClick={() => onClickF()}>Test 2</button>
</div> )