我正在做Wes-Bos教程,並考慮從StorePicker提交表單後重定向到App.js組件的另一種方法。如何在表單onSubmit之後重定向到新組件?
我StorePicker組件:
class StorePicker extends React.Component {
goToStore(event) {
event.preventDefault();
const storeId = this.storeInput.value;
return <Redirect to={`/store/${storeId}`} />;
}
render() {
return (
<Grid textAlign='center' columns={2}>
<Grid.Row>
<Grid.Column><br/>
<Segment>
<Form onSubmit={(e) => this.goToStore(e)} >
<Form.Field >
<h1>Store Name</h1>
<input type="text" required
placeholder="Store Name"
ref={(input) => {this.storeInput = input}}
defaultValue={getFunName()} />
</Form.Field>
<Button fluid type='submit'>Visit Store -></Button>
</Form>
</Segment>
</Grid.Column>
</Grid.Row>
</Grid>
)
}
}
我期待通過陣營路由器的文檔,並試圖玩弄它的<Redirect />
似乎並沒有工作。任何建議如何管理這個重定向?謝謝
你可以顯示代碼在哪裏使用'storeId'參數渲染選定的商店(大概通過''組件)? –
Jaxx
我不是100%確定你在問什麼,但是我的'const storeId = this.storeInput.value;'是在'goToStore();' – karolis2017
中定義的;我的意思是'/ store /:storeId'路由到的組件,而且顯然你在哪裏使用'storeId'參數。 – Jaxx