英文:
Issue in NextJS / ReactJS while clicking on a button to load a different component into the current component
问题
在NextJS中,我有3个组件"Sidebar","Woven"和"ToolsPage",下面附上了各自的代码。
ToolsPage:
import Woven from './components/weaved';
import Sidebar from './sidebar';
import { useState } from 'react';
const ToolsPage = () => {
const [selectedTool, setSelectedTool] = useState('');
const handleToolClick = (tool: string) => {
setSelectedTool(tool);
};
const renderToolComponent = () => {
if (selectedTool === 'weaviate') {
console.log("inside render");
return <Woven />;
}
return <></>;
};
return (
<>
<Sidebar onToolClick={handleToolClick} />
<div>
{selectedTool ? (
// Render the selected tool component
<>
<div>{selectedTool}</div>
{renderToolComponent()}
</>
) : (
<></>
)}
</div>
</>
);
};
export default ToolsPage;
Woven工具组件:
import { useState } from "react";
const Woven = async () => {
const [weaveApiKey, setWeaveApiKey] = useState('');
const [weaveURL, setWeaveURL] = useState('');
return (
<>
<form>
<label>
Weaviate API key:
<input
type="text"
value={weaveApiKey}
onChange={(e) => setWeaveApiKey(e.target.value)}
/>
</label>
<br />
<label>
Weaviate Cluster URL:
<input
type="text"
value={weaveURL}
onChange={(e) => setWeaveURL(e.target.value)}
/>
</label>
<br />
</form>
</>
);
};
export default Woven;
Sidebar组件:
type SidebarProps = {
onToolClick: (tool: string) => void;
};
const Sidebar: React.FC<SidebarProps> = ({ onToolClick }) => {
const toolNames = ['weaviate']; // 添加其他工具名称
return (
<div>
<h2>Tools</h2>
{toolNames.map((tool, index) => (
<button key={index} onClick={() => onToolClick(tool)}>
{tool}
</button>
))}
</div>
);
};
export default Sidebar;
这是你提供的代码的翻译部分。如果需要进一步的帮助或有其他问题,请随时提出。
英文:
In NextJS, I have 3 components "Sidebar", "Woven" and "ToolsPage", the respective codes are attached below.
ToolsPage:
"use client"
import Woven from './components/weaved';
import Sidebar from './sidebar';
import { useState } from 'react';
const ToolsPage = () => {
const [selectedTool, setSelectedTool] = useState('');
const handleToolClick = (tool: string) => {
setSelectedTool(tool);
};
const renderToolComponent = () => {
if (selectedTool === 'weaviate') {
console.log("inside render")
return <Woven />;
}
return <></>;
};
return (
<>
<Sidebar onToolClick={handleToolClick} />
<div>
{selectedTool ? (
// Render the selected tool component
<>
<div>{selectedTool}</div>
{renderToolComponent()}
</>
) : (
<></>
)}
</div>
</>
);
};
export default ToolsPage;
Woven tool component:
"use client"
import { useState } from "react"
const Woven = async () => {
const [weaveApiKey,setWeaveApiKey] = useState('')
const [weaveURL,setWeaveURL] = useState('')
return(
<>
<form>
<label>
Weaviate API key:
<input
type="text"
value={weaveApiKey}
onChange={(e) => setWeaveApiKey(e.target.value)}
/>
</label>
<br/>
<label>
Weaviate Cluster URL:
<input
type="text"
value={weaveURL}
onChange={(e) => setWeaveURL(e.target.value)}
/>
</label>
<br/>
</form>
</>
)}
export default Woven
Sidebar component:
"use client"
type SidebarProps = {
onToolClick: (tool: string) => void;
};
const Sidebar: React.FC<SidebarProps> = ({ onToolClick }) => {
const toolNames = ['weaviate']; // Add other tool names as needed
return (
<div>
<h2>Tools</h2>
{toolNames.map((tool,index) => (
<button key= {index} onClick={() => onToolClick(tool)}>
{tool}
</button>
))}
</div>
);
};
export default Sidebar;
Here I hardcoded only one tool called "weaviate"
This is the sample output screen if I click on the weaviate button (The component did not appear):
My main motive here is to load the Woven component in the ToolsPage on clicking the weaviate button as shown in the picture.
Here the code enters the renderComponent() function successfully (as shown using console log statement in ToolsPage component but it is repeating multiple times continuously and screen got struck)
But the Woven component is not appearing after clicking on the hardcoded weaviate button.
I tried another case- I initiated the state directly to "weaviate" in the ToolsPage line 7
const [selectedTool, setSelectedTool] = useState('weaviate');
Then the component appeared:
But on clicking the button gives the same issue.
Final target: On clicking the button should load the Woven component without the multiple re-renderings
答案1
得分: 2
尝试创建一个独立的组件用于 renderToolComponent
并将 selectedTool
作为其 props 传递。还要从 Woven
组件中删除 async
,然后再试一次。如果您想在该组件中发出 API 调用以从服务器端获取数据并进行渲染,那么请按照此处说明在组件声明之前定义一个异步函数:https://nextjs.org/docs/app/building-your-application/data-fetching/fetching
英文:
Try creating a separate component for the renderToolComponent
and pass selectedTool
as its props. Also, remove async
from the Woven
component and try again and if you want to make an API call in that component to fetch data on server side and render it then define an async function above the component declaration as explained here: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论