react組件傳值冰肴,大概有下面幾種方法:
props
context
redux
react-router 路由切換時通過url傳值(少量非機密數(shù)據(jù)屈藐,其實也是props傳)。
一熙尉、父組件傳給子組件
父組件通過props傳遞給子組件联逻;
//父組件中
<Child data={[1,2,3]} />
//子組件中
console.log(this.props.data);
二、子組件傳給父組件
父組件通過props向子組件傳入一個方法检痰,子組件在通過調(diào)用該方法包归,將數(shù)據(jù)以參數(shù)的形式傳給父組件,父組件可以在該方法中對傳入的數(shù)據(jù)進行處理铅歼;
//父組件
import Child from './Child.js'公壤;
export default class Parent extend compenent{
getData=(data)=>{
console.log(data);
}
render(){
return (
<div>
父組件
<Child getData={this.getData}/>
</div>
)
}
}
//子組件
export default class Child extend compenent{
state={
data:[1,2,3]
}
render(){
const {data}=this.state;
return (
<div>
子組件
<button onClick={()=>{this.props.getData(data)}}><button>
</div>
)
}
}
三,路由傳值椎椰。
1.props.params(推薦)
//設置路由
<Router history={hashHistory}>
<Route path='/user/:name' component={UserPage}></Route>
</Router>
import { Router,Route,Link,hashHistory} from 'react-router';
class App extends React.Component {
render() {
return (
<Link to="/user/sam">用戶</Link>
// 或者
hashHistory.push("/user/sam");
)
}
}
當頁面跳轉(zhuǎn)到UserPage頁面之后厦幅,取出傳過來的值:
export default class UserPage extends React.Component{
constructor(props){
super(props);
}
render(){
return(<div>this.props.match.params.name</div>)
}
}
上面的方法可以傳遞一個或多個值,但是每個值的類型都是字符串慨飘,沒法傳遞一個對象,如果傳遞的話可以將json對象轉(zhuǎn)換為字符串确憨,然后傳遞過去,傳遞過去之后再將json字符串轉(zhuǎn)換為對象將數(shù)據(jù)取出來
//定義路由
<Route path='/user/:data' component={UserPage}></Route>
//設置參數(shù)
var data = {id:3,name:sam,age:36};
data = JSON.stringify(data);
var path = `/user/${data}`;
//傳值
<Link to={path}>用戶</Link>
//或
hashHistory.push(path);
//獲取參數(shù)
var data = JSON.parse(this.props.params.data);
var {id,name,age} = data;
2.query(不推薦:刷新頁面參數(shù)丟失)
query方式使用很簡單瓤的,類似于表單中的get方法缚态,傳遞參數(shù)為明文
//定義路由
<Route path='/user' component={UserPage}></Route>
//設置參數(shù)
var data = {id:3,name:sam,age:36};
var path = {
pathname:'/user',
query:data,
}
//傳值
<Link to={path}>用戶</Link>
//或
hashHistory.push(path);
//獲取參數(shù)
var data = this.props.location.query;
var {id,name,age} = data;
3.state(不推薦,刷新頁面參數(shù)丟失)
state方式類似于post方式,使用方式和query類似
//設置路由
<Route path='/user' component={UserPage}></Route>
//設置參數(shù)
var data = {id:3,name:sam,age:36};
var path = {
pathname:'/user',
state:data,
}
//傳值
<Link to={path}>用戶</Link>
//或
hashHistory.push(path);
//獲取參數(shù)
var data = this.props.location.state;
var {id,name,age} = data;
特別提示:
1,獲取參數(shù)時要用this.props.match.params.name
2弧腥,如果在子組件里打印要記得傳this.props,如下:
class Todolist extends Component {
render() {
return (
<DocumentTitle title="todolist">
<div id="home-container">
<section>
<TodolistList {...this.props}/> //不傳的話this.props為空對象
</section>
</div>
</DocumentTitle>
);
}
}
export default Todolist;
四桥帆,非父子組件且嵌套關(guān)系復雜的組件之間數(shù)據(jù)的傳遞。
通過redux來傳值的實現(xiàn)
1慎皱,我們先寫一個createAction的函數(shù)
export function setAnalysisParams(params) {
return {
type: SET_ANALYSIS_PARAMS,
result: params
}
}
2老虫,在reducer里面:
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case SET_ANALYSIS_PARAMS:
return {
...state,
params: action.result
};
default:
return state;
}
當action觸發(fā)reducer時,會把action的result傳給reducer的params茫多。寫好這里祈匙,我們就可以在組件中傳給adction params了。
@connect(
() => ({
}),
{
setAnalysisParams
})
先把actionCreator拿出來。
在組件的某個需要的地方夺欲,可以直接向它傳我們要放進redux里的數(shù)據(jù):
this.props.setAnalysisParams({
someModels
});
這時跪帝,我們就可以在另外一個組件中取到剛剛放進去的數(shù)據(jù)。
另外一個組件:
@connect(
state => ({
example: state.clinic.params
}),
{}
)
把redux中的params數(shù)據(jù)映射到example上些阅。
下面伞剑,就可以用了:
const {someNames, ...} = this.props.example; //取出數(shù)據(jù)名