나는 ReactJs에서 응용 프로그램을 개발하고 있어요 지금은에서 클릭 된 ID 전달해야 Main.js의 페이지를 Sofa.js 그냥 클릭하는 ID에 따라 다른 정보를 표시하는 것이다. 내 밍은 모두 PHP로 포맷되어 있으며 ReactJs에 $ _GET ahah와 같은 전역 변수가 있기를 바랍니다. 아마도 그럴 수도 있습니다. 지금은 안됩니다. 나는 수색을했지만 내가 찾고있는 것을 찾지 못했다. 아래 코드를 보여 드리겠습니다.
가져 오기가 포함되지 않은 Main.js 코드 :
export class Main extends React.Component {
constructor(props) {
super(props);
this.state = {
token: {},
isLoaded: false,
models: [],
offset: offset,
limit: limit
};
}
componentDidMount() {
/* Some code not relevant to the question, for getting API token */
fetch('url/couch-model?offset=' + offset + '&limit=' + limit, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'JWT ' + (JSON.parse(localStorage.getItem('token')).token)
}
}).then(res => {
if (res.ok) {
return res.json();
} else {
throw Error(res.statusText);
}
}).then(json => {
this.setState({
models: json.results,
isLoaded: true
}, () => { });
})
}
render() {
const { isLoaded, models } = this.state;
if (!isLoaded) {
return (
<div id="LoadText">
Loading...
</div>
)
} else {
return (
<div>
{models.map(model =>
<a href={"/sofa?id=" + model.id} key={model.id}>
<div className="Parcelas">
<img src={"url" + model.image} className="ParcImage" alt="sofa" />
<h1>{model.name}</h1>
<p className="Features">{model.brand.name}</p>
<button className="Botao">
<p className="MostraDepois">Ver Detalhes</p>
<span>+</span>
</button>
<img src="../../img/points.svg" className="Decoration" alt="points" />
</div>
</a>
)}
<PageButtons offset={offset} />
</div>
)
}
}
}
보시다시피 <a>
두 번째 반환은 올바른 방법인지 확실하지 않지만 ID를 보냅니다. 내가 쓰려고 시도한 것은 route-js와 같은 경로 /sofa/:id
이지만 어쨌든 / sofa / 1 내부의 CSS는 ID가 무엇이든 작동을 멈췄습니다.
이제 Sofa.js 코드, 가져 오기 없음 및 관련 코드 만 :
export class Sofa extends React.Component {
constructor(props) {
super(props);
this.state = {
token: {},
isLoaded: false,
model: {}
};
}
componentDidMount() {
fetch(url + '/couch-model/1{/*this show be id clicked and sent by url*/}/', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'JWT ' + (JSON.parse(localStorage.getItem('token')).token)
}
}).then(res => {
if (res.ok) {
return res.json();
} else {
throw Error(res.statusText);
}
}).then(json => {
this.setState({
model: json,
isLoaded: true
}, () => {});
})
}
render() {
const { model, isLoaded } = this.state;
if (!isLoaded) {
return (
<div id="LoadText">
Estamos a preparar o seu sofá!
</div>
)
} else {
return (
<div id="Esquerda">
<h2>{model.area_set.map(area => area.name)}</h2>
<h1>{model.name}</h1>
<p>Highly durable full-grain leather which is soft and has a natural look and feel.</p>
<h3>Design</h3>
<h4>Hexagon</h4>
</div>
);
}
}
}
또한 여기 route.js가 있습니다.
const Routes = (props) => (
<BrowserRouter>
<Switch>
<Route path='/sofa/:id' component={Sofa}/>
<Route path='/home' component={Home}/>
<Route path='*' component={NotFound}/>
</Switch>
</BrowserRouter>
);
export default Routes;
나는 당신이 (예제에서 볼 수있는) react-router를 사용하지 않는 것을 봅니다.
이런 상황을 아주 쉽게 만들 수 있기 때문에 사용하는 것이 좋습니다.
이 예 에서처럼
const ParamsExample = () => (
<Router>
<div>
<h2>Accounts</h2>
<ul>
<li>
<Link to="/netflix">Netflix</Link>
</li>
<li>
<Link to="/zillow-group">Zillow Group</Link>
</li>
<li>
<Link to="/yahoo">Yahoo</Link>
</li>
<li>
<Link to="/modus-create">Modus Create</Link>
</li>
</ul>
<Route path="/:id" component={Child} />
</div>
</Router>
);
그리고 Child 컴포넌트는 경로에 의해 주입 된 일치를 가지고 있습니다.
const Child = ({ match }) => (
<div>
<h3>ID: {match.params.id}</h3>
</div>
);
귀하의 경우 소파 는 하위 구성 요소입니다. URL에서받은 ID로 componentDidMount에서 원하는 API를 호출 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다