将内容从html转换为ReactJS文件

Converting contents from html to ReactJS file

本文关键字:ReactJS 文件 转换 html      更新时间:2023-09-26

如果问题令人困惑,请道歉。基本上我有这个html代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>A Gentle Introduction</title>
    <script
      src="https://rawgit.com/flatiron/director/master/build/director.min.js">
    </script>
    <script>
      var author = function () { console.log("author"); };
      var books = function () { console.log("books"); };
      var viewBook = function (bookId) {
        console.log("viewBook: bookId is populated: " + bookId);
      };
      var routes = {
        '/author': author,
        '/books': [books, function() {
          console.log("An inline route handler.");
        }],
        '/books/view/:bookId': viewBook
      };
      var router = Router(routes);
      router.init();
    </script>
  </head>
  <body>
    <ul>
      <li><a href="#/author">#/author</a></li>
      <li><a href="#/books">#/books</a></li>
      <li><a href="#/books/view/1">#/books/view/1</a></li>
    </ul>
  </body>
</html>

它清楚地存在于.html文件中。我想把它改成一个.js文件,这样我就可以把html放在js中,这样当点击不同的链接时,路由/返回的内容就不同了。

我真的不知道如何直接将其放入javascript文件中,然后让路由器工作。这就是html文件的来源https://github.com/flatiron/director#client-侧路由,我正在尝试使用这个熨斗/控制器路由器。

任何帮助都会很棒!

我能够使用react和jsx以及react本身之外的路由代码。

使用es6/es2015 编写

app.js

const author = () => { console.log("author"); };
const books = () => { console.log("books"); };
const viewBook = (bookId) => { console.log("viewBook: bookId is populated: " + bookId); };
const routes = {
  '/author': author,
  '/books': [books, () => { console.log("An inline route handler."); }],
  '/books/view/:bookId': viewBook
};
const router = Router(routes);
router.init();
class SampleRouting extends React.Component {
  render() {
    return (
      <ul>
        <li><a href="#/author">#/author</a></li>
        <li><a href="#/books">#/books</a></li>
        <li><a href ="#/books/view/1">#/books/view/1</a></li>
      </ul>
    )
  }
}
React.render( <SampleRouting/> , document.getElementById('root'));

index.html

<div id="root"></div>

示例:http://s.codepen.io/oobgam/debug/vNoogO

_编辑app.js以反映状态和页头的更新

class App extends React.Component {
 constructor(props) {
    super(props);
    this.state = { currentPage: 'author' }
 }
  componentDidMount() {
    const author = () => { this.setState({currentPage: 'author'}) };
    const books = () => { this.setState({currentPage: 'Books'}); };
    const viewBook = (bookId) => { this.setState({currentPage: 'Book ' + bookId }); };
    const routes = {
      '/author': author,
      '/books': books,
      '/books/view/:bookId': viewBook
    };
    const router = Router(routes);
    router.init();
  }
  render() {
    return (
      <div>
        <h1>{ this.state.currentPage }</h1>
        <SampleRouting />
      </div>
    );
  }
}
// stateless function
const SampleRouting = () => {
   return (
      <ul>
        <li><a href="#/author">#/author</a></li>
        <li><a href="#/books">#/books</a></li>
        <li><a href ="#/books/view/1">#/books/view/1</a></li>
      < /ul>
    )
}