将导航道具反应为 header
React navigation props to header
如果有人创建了一个房间,他就会被重定向到这个房间,我会像这样将参数传递给它:
useEffect(() => {
createdGroup === true ? navigation.navigate('Room', { roomIdent }) : null;
}, [createdGroup]);
但是我定制了一个header。我如何将参数传递给 header?
Stack.js
...
<Stack.Screen
name="Room"
component={Room}
options={
{
headerTitle: () => <HeaderRoom />,
headerLeft: null,
headerStyle: {
elevation: 0,
borderBottomWidth: 0
}
}
}
/>
您需要提升状态。用官方 React 文档看这个例子
class Calculator extends React.Component {
constructor(props) {
super(props);
this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
this.state = {temperature: '', scale: 'c'}; }
handleCelsiusChange(temperature) {
this.setState({scale: 'c', temperature}); }
handleFahrenheitChange(temperature) {
this.setState({scale: 'f', temperature}); }
render() {
const scale = this.state.scale; const temperature = this.state.temperature; const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature; const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
return (
<div>
<TemperatureInput
scale="c"
temperature={celsius} onTemperatureChange={this.handleCelsiusChange} /> <TemperatureInput
scale="f"
temperature={fahrenheit} onTemperatureChange={this.handleFahrenheitChange} /> <BoilingVerdict
celsius={parseFloat(celsius)} /> </div>
);
}
}
您可以在这里阅读更多内容:https://reactjs.org/docs/lifting-state-up.html
工作示例here
像这样为 Room
制作你的 Stack.Screen
<Stack.Screen
name="Room"
component={Room}
options={({ route }) => ({
headerTitle: () => <HeaderRoom Title={route.params.name} />,
headerLeft: null,
headerStyle: {
elevation: 0,
borderBottomWidth: 0,
},
})}
/>
然后在你的HeaderRoom.js
中像这样使用它
import React from 'react';
import { Text } from 'react-native';
function HeaderRoom(props) {
console.log(props.Title) // it will log that custom title
return (
<Text style={{ fontWeight: 'bold', fontSize: 20 }}>{props.Title}</Text>
);
}
export default HeaderRoom;
如果有人创建了一个房间,他就会被重定向到这个房间,我会像这样将参数传递给它:
useEffect(() => {
createdGroup === true ? navigation.navigate('Room', { roomIdent }) : null;
}, [createdGroup]);
但是我定制了一个header。我如何将参数传递给 header?
Stack.js
...
<Stack.Screen
name="Room"
component={Room}
options={
{
headerTitle: () => <HeaderRoom />,
headerLeft: null,
headerStyle: {
elevation: 0,
borderBottomWidth: 0
}
}
}
/>
您需要提升状态。用官方 React 文档看这个例子
class Calculator extends React.Component {
constructor(props) {
super(props);
this.handleCelsiusChange = this.handleCelsiusChange.bind(this);
this.handleFahrenheitChange = this.handleFahrenheitChange.bind(this);
this.state = {temperature: '', scale: 'c'}; }
handleCelsiusChange(temperature) {
this.setState({scale: 'c', temperature}); }
handleFahrenheitChange(temperature) {
this.setState({scale: 'f', temperature}); }
render() {
const scale = this.state.scale; const temperature = this.state.temperature; const celsius = scale === 'f' ? tryConvert(temperature, toCelsius) : temperature; const fahrenheit = scale === 'c' ? tryConvert(temperature, toFahrenheit) : temperature;
return (
<div>
<TemperatureInput
scale="c"
temperature={celsius} onTemperatureChange={this.handleCelsiusChange} /> <TemperatureInput
scale="f"
temperature={fahrenheit} onTemperatureChange={this.handleFahrenheitChange} /> <BoilingVerdict
celsius={parseFloat(celsius)} /> </div>
);
}
}
您可以在这里阅读更多内容:https://reactjs.org/docs/lifting-state-up.html
工作示例here
像这样为 Room
制作你的 Stack.Screen
<Stack.Screen
name="Room"
component={Room}
options={({ route }) => ({
headerTitle: () => <HeaderRoom Title={route.params.name} />,
headerLeft: null,
headerStyle: {
elevation: 0,
borderBottomWidth: 0,
},
})}
/>
然后在你的HeaderRoom.js
中像这样使用它
import React from 'react';
import { Text } from 'react-native';
function HeaderRoom(props) {
console.log(props.Title) // it will log that custom title
return (
<Text style={{ fontWeight: 'bold', fontSize: 20 }}>{props.Title}</Text>
);
}
export default HeaderRoom;