2016-03-31 7 views
5

Próbuję skopiować ten skrzypce: http://jsfiddle.net/jhudson8/135oo6f8/onClick obsługi, nie rejestrując się ReactDOMServer.renderToString

(Próbowałem też ten przykład http://codepen.io/adamaoc/pen/wBGGQv i tym samym onClick obsługi Problem istnieje)

i zrobić na skrzypcach pracować dla renderowania po stronie serwera, używając ReactDOMServer.renderToString

mam to wezwanie:

res.send(ReactDOMServer.renderToString((
      <html> 
      <head> 

       <link href={'/styles/style-accordion.css'} rel={'stylesheet'} type={'text/css'}></link> 

      </head> 

      <body> 


      <Accordion selected='2'> 
       <AccordionSection title='Section 1' id='1'> 
        Section 1 content 
       </AccordionSection> 
       <AccordionSection title='Section 2' id='2'> 
        Section 2 content 
       </AccordionSection> 
       <AccordionSection title='Section 3' id='3'> 
        Section 3 content 
       </AccordionSection> 
      </Accordion> 
      </body> 
      </html> 
     ))); 

element Akordeon wygląda tak:

const React = require('react'); 

const fs = require('fs'); 
const path = require('path'); 


const Accordion = React.createClass({ 

    getInitialState: function() { 
     // we should also listen for property changes and reset the state 
     // but we aren't for this demo 
     return { 
      // initialize state with the selected section if provided 
      selected: this.props.selected 
     }; 
    }, 

    render: function() { 

     // enhance the section contents so we can track clicks and show sections 
     const children = React.Children.map(this.props.children, this.enhanceSection); 

     return (
      <div className='accordion'> 
       {children} 
      </div> 
     ); 
    }, 

    // return a cloned Section object with click tracking and 'active' awareness 
    enhanceSection: function (child) { 

     const selectedId = this.state.selected; 
     const id = child.props.id; 

     return React.cloneElement(child, { 
      key: id, 
      // private attributes/methods that the Section component works with 
      _selected: id === selectedId, 
      _onSelect: this.onSelect 
     }); 
    }, 

    // when this section is selected, inform the parent Accordion component 
    onSelect: function (id) { 
     this.setState({selected: id}); 
    } 
}); 


module.exports = Accordion; 

i składnik AccordionSection wygląda tak:

const React = require('react'); 


const AccordionSection = React.createClass({ 

    render: function() { 

     const className = 'accordion-section' + (this.props._selected ? ' selected' : ''); 

     return (
      <div className={className}> 
       <h3 onClick={this.onSelect}> 
        {this.props.title} 
       </h3> 
       <div className='body'> 
        {this.props.children} 
       </div> 
      </div> 
     ); 
    }, 

    onSelect: function (e) { 
     console.log('event:',e); 
     // tell the parent Accordion component that this section was selected 
     this.props._onSelect(this.props.id); 
    } 
}); 



module.exports = AccordionSection; 

wszystko działa, a CSS działa, ale problemem jest to, że onClick nie zostaje zarejestrowany. Kliknięcie na elementy akordeonu nic nie robi. Czy ktoś wie, dlaczego handler onClick może nie zostać zarejestrowany w tej sytuacji?

+0

jest JS działa w ogóle? czy któryś z JS pracuje na kliencie? –

+0

myślę brakującym elementem jest to, że na kliencie, gdy znaczniki dostaje tam musimy zrobić kilka React połączenia, które rzeczywiście wiążą teleskopowe –

Odpowiedz

5

React DOM render na ciąg tylko wysyła początkowy HTML jako ciąg bez żadnego JS.
Potrzebny jest również router reagujący po stronie klienta, który będzie dołączał wymagane procedury obsługi JS do kodu HTML na podstawie ich identyfikatorów reagowania. JS musi działać po obu stronach.
Uniwersalny boilerplate renderowania dla szybkiego startu. https://github.com/erikras/react-redux-universal-hot-example
Kolejne pytanie podobne do twojego. React.js Serverside rendering and Event Handlers

4

Żaden z hakami zarejestruje z ReactDOMServer.RenderToString. Jeśli chcesz osiągnąć boczny renderingu + haki serwera na swojej reakcji składnika, można zapakować go na kliencie (WebPack, haustem, cokolwiek), a następnie również użyć ReactDOMServer.RenderToString na serwerze.

Oto blogu, że pomogło mi to osiągnąć: https://www.terlici.com/2015/03/18/fast-react-loading-server-rendering.html

Powiązane problemy