我在使用React在两个组件之间进行通讯时遇到麻烦。
以下是代码:
档案Search.js
:
const Search = (props) => {
const [items, setItems] = React.useState(null);
const [input, setInput] = React.useState("");
const apiKey = "*********";
async function handleSubmit(event) {
event.preventDefault();
await fetch(
`https://geo.ipify.org/api/v1?apiKey=${apiKey}&ipAddress=${input}`
)
.then((response) => {
console.log(response);
return response.json();
})
.then((result) => {
console.log(result);
setItems(result);
});
}
const handleChange = (event) => {
setInput(event.target.value);
};
return (
<div>
<form onSubmit={handleSubmit} className="my-wrap">
<input
type="text"
className="searchTerm"
placeholder="Search for any IP adress or domain"
value={input}
onChange={handleChange}
/>
<button type="submit" className="searchButton">
{" "}
<img src={RightIcon} alt="icon" />{" "}
</button>
</form>
</div>
);
};
export default Search;
档案Results.js
:
const Results = (props) => {
const { ip, city, country, postalCode, timezone, isp } = props;
return (
<div>
<div className="grid-container2">
<div className="container-pop">
<div>
<h2 className="title-span"> IP Address </h2>
<p> {ip} </p>
</div>
<div>
<h2 className="title-span"> Location </h2>
<p>
{city}, {country}
</p>
<p> {postalCode} </p>
</div>
<div>
<h2 className="title-span"> Timezone </h2>
<p> UTC {timezone} </p>
</div>
<div>
<h2 className="title-span"> ISP </h2>
<p> {isp} </p>
</div>
</div>
</div>
</div>
);
};
export default Results;
我无法进行的是它们之间的通信,例如:我进行了Google IP搜索8.8.8.8,控制台将信息返回给我,但我无法将此信息传递给Results.js
,以获取它们并将它们打印在画布上。如果我在同一组件(Search.js
)中执行此操作,则它会显示在屏幕上,但是CSS的样式会变得混乱。有没有办法在这两个组件之间进行这种通信?
没有看到更多代码,我不能百分百确定此处,但是看来你需要做的就是将你的items
状态提升为<Search>
和的父级<Results>
function SearchPage() {
const [results, setResults] = React.useState([])
return (
<div>
<Search onChange={setResults) />
<div>
{results.map((result) => <Results {...result} />)}
</div>
</div>
)
}
而且你需要改变 Search.js
const Search = (props) => {
// New prop! This is a change handler passed from the parent
const { onChange } = props
// remove the results data from state here
const [input, setInput] = React.useState("");
const apiKey = "*********";
async function handleSubmit(event) {
event.preventDefault();
await fetch(
`https://geo.ipify.org/api/v1?apiKey=${apiKey}&ipAddress=${input}`
)
.then((response) => {
console.log(response);
return response.json();
})
.then((result) => {
console.log(result);
// Instead of setting state, we call the change handler function.
onChange(result);
});
}
const handleChange = (event) => {
setInput(event.target.value);
};
return (
// ... same return value
);
};
export default Search;
当然,你需要把这个正确的标记,以便它的风格你喜欢的方式,但这里最重要的是,我们从把国家Search
和我们提起这两者的共同父Search
和Results
。这是React中非常常见的技术。从公共父级,我们可以将更改处理程序传递给,Search
以便在用户输入某些内容后获得API请求的结果,然后可以将其设置为父级组件的状态。从那里,我们可以将其传递到结果中。这就是React的方式:数据作为道具流下,你将变更处理程序函数作为道具传递,以从子组件中获取数据。
<SearchPage> {/* <---- put the state at this level. the next highest above Search and Results, so you can pass change handlers and data (props) _down_ into the components. */}
<Search />
<Results />
<Results />
<Results />
<Results />
</SearchPage>
那正是我想要的!通过像您提到的那样进行调用,我能够将API结果显示在输出显示中。谢谢!!