[英]Pass Object From One Component To Another In React
I am new to React so any help would be appreciated ! 我是React的新手,所以我们将不胜感激!
UserPage.js : UserPage.js:
import React, { Component } from 'react';
import { Link } from 'react-router';
import request from 'superagent';
export default class UserPage extends Component {
constructor(props) {
super(props);
this.state = {
listSchool: []
};
}
componentWillMount() {
let self = this;
request
.get('/api/schools/')
.set('Accept', 'application/json')
.end(function (err, res) {
self.setState({ 'listSchool': res.body });
console.log(self);
});
}
render() {
let marginLeft = {
marginLeft: "10px"
};
return (
<div className="container">
<form className="form-inline">
<div className="form-group">
Xin chào: <label style={marginLeft}>{this.props.location.state.name} </label>
<div className="btn-group" style={marginLeft}>
<button type="button" className="btn btn-info">Choose Your School</button>
<button type="button" className="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span className="caret"></span>
<span className="sr-only">Toggle Dropdown</span>
</button>
<ul className="dropdown-menu">
{
this.state.listSchool.map(school => {
return (
<li key={school._id}>
<Link to={`schools/${school._id}`}
params={
{
user : {
id : this.props.location.state.id,
name : this.props.location.state.name,
},
school : {
id : school._id,
name : school.TenTruong
}
}
}>
{school.TenTruong}
</Link>
</li>
);
})
}
</ul>
</div>
</div>
</form>
</div>
);
}
}
SchoolPage.js : SchoolPage.js:
import React, { Component } from 'react';
export default class SchoolPage extends Component {
constructor(props) {
super(props);
this.state = {
user : {},
school : {},
};
}
componentWillMount() {
console.log(this.props);
}
render() {
return (
<div>
Hello {this.props.params.id}
</div>
);
}
}
routes.js : route.js:
import React from 'react';
import { Route, IndexRoute } from 'react-router';
import Layout from './components/Layout';
import IndexPage from './components/IndexPage';
import UserPage from './components/UserPage';
import SchoolPage from './components/SchoolPage';
const routes = (
<Route path="/" component={Layout}>
<IndexRoute component={IndexPage}></IndexRoute>
<Route path="users/:id" component={UserPage}></Route>
<Route path="schools/:id" component={SchoolPage}></Route>
</Route>
);
export default routes;
How can I pass the object from Link tag from UserPage Component to SchoolPage Component and use it. 如何将对象从链接标记从UserPage组件传递到SchoolPage组件并使用它。
I have searched and knew that was impossible to pass an object through Link tag so in React how can I pass it. 我已经搜索过并且知道不可能通过Link标签传递对象,因此在React中如何传递它。 Tks for your help!
谢谢你的帮助!
You should export Route from react-router. 您应该从react-router导出Route。 See the example You can see as the children component uses this.props.params.
请参见示例,您可以看到子组件使用this.props.params。
You can use Redux store. 您可以使用Redux存储。 You need to configure store, add dispatch action and reducer.
您需要配置存储,添加调度动作和减速器。 And then you can to send data between any components:
然后,您可以在任何组件之间发送数据:
UserPage UserPage
1) import your sendObjectToSchoolPage dispatch action 1)导入sendObjectToSchoolPage调度动作
2) add mapDispatchToProps function 2)添加mapDispatchToProps函数
function mapDispatchToProps(dispatch) {
return {
passMyOwnObject(obj) {
dispatch(sendObjectToSchoolPage(obj)); // sendObjectToSchoolPage is your dispatch action
}
};
}
and from your UserPage component you invoke this dispatch action like that: 然后从您的UserPage组件中调用此调度操作,如下所示:
this.props.passMyOwnObject({ name : ABC });
SchoolPage SchoolPage
You need to define: 您需要定义:
1) myOwnObject as a PropTypes.object 1)myOwnObject作为PropTypes.object
2) add mapStateToProps function to your component 2)将mapStateToProps函数添加到您的组件
function mapStateToProps(state){ // this allows you to get any object from Redux store
return {
myOwnObject: getMyOwnObjectFromStore(state) // getMyOwnObjectFromStore is your reducer
};
}
and by that you can in your component SchoolPage get your object by using: 这样您就可以在组件SchoolPage中使用以下方法获取对象:
const myObject = this.props.myOwnObject; // myObject has { name : ABC }
By this approach you need to learn how to configure your Redux store, create your dispatch action and create own reducers. 通过这种方法,您需要学习如何配置Redux商店,创建调度动作以及创建自己的reducer。
Good luck! 祝好运!
If you are trying to implement authentication and then trying to save the name of the user or similar details to use further you can either use localStorage eg 如果您尝试实现身份验证,然后尝试保存用户名或类似详细信息以进一步使用,则可以使用localStorage,例如
localStorage.name = data.name
localStorage.email = data.email
OR 要么
you can fetch/get these values in the layout component and when you render UserPage or SchoolPage using {this.props.children} u can use React.cloneElement to add those fetched data as props to all the rendered components. 您可以在布局组件中获取/获取这些值,并在使用{this.props.children}渲染UserPage或SchoolPage时,可以使用React.cloneElement将获取的数据作为道具添加到所有渲染的组件中。 eg inside Layout
例如内部布局
render(){
const childrenWithProps = React.Children.map(this.props.children,
(child) => React.cloneElement(child, {
name: data.name,
email: data.email
})
);
return (
<div>{childrenwithprops}</div>)
}
using redux for this would clearly be an overkill by the complexity it adds seeing you are just starting with react. 显然,使用redux会显得过高,因为它增加了复杂性,使您看到只是从反应开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.