我试图将AdminNavBar.js中“ value”的值传递给Sentiment.js,并替换 ticker 变量中的值。我真的是React的新手。任何更好的方法也都很好。
我真正需要的是,搜索一张票证,该状态将传递给Sentiment.js,它将代替股票代码。
非常感谢
AdminNavBar.js
import React from "react";
class AdminNavbar extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
console.log('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<>
<Navbar className="navbar-top navbar-dark" expand="md" id="navbar-main">
<Container fluid>
<Link
className="h4 mb-0 text-white text-uppercase d-none d-lg-inline-block"
to="/"
>
{this.props.brandText}
</Link>
<Form className="navbar-search navbar-search-dark form-inline mr-3 d-none d-md-flex ml-lg-auto" onSubmit={this.handleSubmit}>
<FormGroup className="mb-0">
<InputGroup className="input-group-alternative">
<InputGroupAddon addonType="prepend">
<InputGroupText>
<i className="fas fa-search" />
</InputGroupText>
</InputGroupAddon>
<Input placeholder="Search" type="text" value={this.state.value} onChange={this.handleChange}/>
</InputGroup>
</FormGroup>
</Form>
<Nav className="align-items-center d-none d-md-flex" navbar>
<UncontrolledDropdown nav>
<DropdownToggle className="pr-0" nav>
<Media className="align-items-center">
<span className="avatar avatar-sm rounded-circle">
<img
alt="..."
src={require("assets/img/theme/team-4-800x800.jpg")}
/>
</span>
<Media className="ml-2 d-none d-lg-block">
<span className="mb-0 text-sm font-weight-bold">
Jessica Jones
</span>
</Media>
</Media>
</DropdownToggle>
<DropdownMenu className="dropdown-menu-arrow" right>
<DropdownItem className="noti-title" header tag="div">
<h6 className="text-overflow m-0">Welcome!</h6>
</DropdownItem>
<DropdownItem to="/admin/user-profile" tag={Link}>
<i className="ni ni-single-02" />
<span>My profile</span>
</DropdownItem>
<DropdownItem to="/admin/user-profile" tag={Link}>
<i className="ni ni-settings-gear-65" />
<span>Settings</span>
</DropdownItem>
<DropdownItem to="/admin/user-profile" tag={Link}>
<i className="ni ni-calendar-grid-58" />
<span>Activity</span>
</DropdownItem>
<DropdownItem to="/admin/user-profile" tag={Link}>
<i className="ni ni-support-16" />
<span>Support</span>
</DropdownItem>
<DropdownItem divider />
<DropdownItem href="#pablo" onClick={e => e.preventDefault()}>
<i className="ni ni-user-run" />
<span>Logout</span>
</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
</Container>
</Navbar>
</>
);
}
}
export default AdminNavbar;
Sentiment.js
import React, {useState,useEffect} from 'react';
import axios from 'axios';
import AdminNavbar from './Navbars/AdminNavbar';
export default function Sentiment(props) {
console.log("*****************************************")
console.log(AdminNavbar.props)
const [result,setResult] = useState(null);
const message = async() =>{
try{
let res = await axios.get('/sentiment-analysis/AAPL');
let result = res.data;
setResult(result);
}catch(e){
console.log(e)
}
}
useEffect(() => {
message()
},[])
return (
<div>
{result}
</div>
)
}
答案 0 :(得分:0)
我建议使用上下文或redux在全局范围内传递状态。